1

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>
schwarz
  • 501
  • 7
  • 28
  • Can you show what you have? What you *want* to work but doesn't? I kind of understand what you are asking, but some code would help. – BradleyDotNET Oct 07 '14 at 17:12
  • Bradley, added some code :) – schwarz Oct 07 '14 at 17:25
  • 2
    Thanks for adding the code. I'm still a bit confused. Could you post the code that you want to work, but doesn't? – BradleyDotNET Oct 07 '14 at 17:28
  • Well its not that something is not working, I just dont know how to use data templates in this scenario! I dont think ItemsControl would work here, right? – schwarz Oct 07 '14 at 17:39
  • Its hard to say; `ItemsControl` works on a collection. What I don't understand is what you are *trying* to do. – BradleyDotNET Oct 07 '14 at 17:40
  • Lets just say I want a window which should have data context as the MyDataContext class and I need to write xaml inside this window xaml file to display each field as a combination of checkbox and slider. – schwarz Oct 07 '14 at 17:43
  • 1
    Ah, I see now. I'll think about the best approach and post soon. – BradleyDotNET Oct 07 '14 at 17:50

1 Answers1

1

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.

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117