Reputation: 521
I have a data context class with complex fields (objects). Each object has a double and boolean field and some more fancy info. I also have two data templates, one for a double value to represent it as a slider and another one to represent a bool field as check box. How do I write the xaml to use the two already existing data templates I have? The problem is that the data context class can have fields which are not those objects containing a double and boolean value.
I am a newbie but I know how bindings and data context works.
Some code:
Class MyDataContext
{
public MyWpfField field1;
public WeirdField field1;
public MyWpfField field1;
public WeirdField field1;
}
Class MyWpfField
{
public string niceName;
public bool isEnable;
public double data;
.
.
.
}
Some xaml:
<DataTemplate x:Key="DataBoolTmpl">
<StackPanel
Margin="{StaticResource DefaultSpacing}"
Orientation="Horizontal"
ToolTip="{Binding ....}">
<CheckBox
IsEnabled="{Binding ...}"
Visibility="{Binding ...}"
IsChecked="{Binding ...}" />
<TextBlock Text="{Binding niceName}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="DataTmplSlider">
<StackPanel
IsEnabled="{Binding ...}"
Margin="{StaticResource DefaultSpacing}"
ToolTip="{Binding ...}" >
<TextBlock Text="{Binding niceName}" />
<StackPanel Orientation="Horizontal"
Margin="{StaticResource DefaultSpacing}">
<TextBlock Text="{Binding ...}" MinWidth="40"/>
<TextBlock Text="{Binding ...}"/>
<Slider
Margin="5,0,0,0"
Visibility="{Binding ...}"
Minimum="{Binding ...}"
Maximum="{Binding ...}"
IsSnapToTickEnabled="True"
SmallChange="{Binding ...}"
MinWidth="100"
MinHeight="15">
<Slider.Value>
<Binding ...}"/>
</Slider.Value>
</Slider>
</StackPanel>
</StackPanel>
</DataTemplate>
Upvotes: 1
Views: 92
Reputation: 61379
Adapted from this answer: https://stackoverflow.com/a/755177/1783619
Use a ContentControl
to utilize your data templates:
<ContentControl ContentTemplate="{StaticResource ..}" Content="{Binding MyField}"/>
You'll create one for each field. For more information, see MSDN.
Upvotes: 1