Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to Data Bind XML Data to a user control Dependency Property

$
0
0

I am new to WPF and am trying to understand how to use XML data to bind to properties of a custom control.   My long term plan is to use external XML data to drive building UI elements.  I have created a user control that is made up of a ToggleButton that has a slider, textbox and label control within it.  I want to be able to add multiple copies of this user control in my main UI and then populate the properties of its controls (Button Content, Slider Min/Max/Value, Label Content) using the XML data.

I think I have everything setup correctly but when I test my application I am not seeing my XML data populate my user control properties.  I don't get any errors it just doesn't seem to be working.   I think the problem is how my Dependency Properties of my user control are setup.

I setup my XML data like this

<Application.Resources><XmlDataProvider x:Key="MyDataSource" Source="MyData.xml" d:IsDataSource="True"/></Application.Resources>

The layout of my user control looks like this

<ToggleButton x:Name="toggleButton" Content="Test Button" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Grid.RowSpan="4" Grid.ColumnSpan="4"><ToggleButton.ContextMenu><ContextMenu><MenuItem Header="Enable" IsCheckable="True" IsChecked="True"/><MenuItem Header="Solo" IsCheckable="True"/></ContextMenu></ToggleButton.ContextMenu></ToggleButton><Slider x:Name="sliderControl" HorizontalAlignment="Center" Height="25"  VerticalAlignment="Top" Width="140" Maximum="100" LargeChange="5" SmallChange="1" TickFrequency="5" TickPlacement="BottomRight" Value="50" Grid.Row="2"  Grid.ColumnSpan="4" Margin="7.5,0" Minimum="0"/><TextBox x:Name="textbox" Text="{Binding ElementName=sliderControl, Path=Value}" Grid.Row="3" Grid.Column="1" Margin="0,0,0,5"  /><Label x:Name="labelControl"  Content="dB" Grid.Row="3" Grid.Column="2"/>


Here is an example of the Dependency property declaration for the user control

	Public Shared ReadOnly ButttonTextProperty as DependencyProperty = DependencyProperty.Register("ButtonText",GetType(String),GetType(ButtonSlider)) 
	Public Property ButtonText() as String
		Get	
			Return GetValue(ButttonTextProperty)
		End Get
		Set(ByVal value as String)
			SetValue(ButttonTextProperty, value)
			toggleButton.Content = value
		End Set
	End Property


Lastly here is how I am referencing the user control in the main application and binding the XML data to it.

<local:ButtonSlider ButtonText="{Binding XPath=MyXMLDATA/ControlRecord/@Name}" LabelText="{Binding XPath=MyXMLDATA/ControlRecord/@Metric}" SliderMax="{Binding XPath=MyXMLDATA/ControlRecord/@Max}" SliderMin="{Binding XPath=MyXMLDATA/ControlRecord/@Min}" SliderValue="{Binding XPath=MyXMLDATA/ControlRecord/@Value}" Height="80" Width="155"/>

Here is a sample of the XML data

<MyXMLDATA><ControlRecord ID="1" Name="Control 1" Enable="True" Solo="False" Min="-12.0" Max="12.0" Metric="dB"/><ControlRecord ID="2" Name="Control 2" Enable="True" Solo="False" Min="0" Max="100" Metric="%"/></MyXMLDATA>

What I would expect to see on my single control I have laid out for the Button Content is "Control 1"  but it is still displaying the "Test Button" default from the user control?

What am I missing or what am I doing wrong?

 

 


Jeff Davis


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>