4

If I have this structure:

public class Parent
{
  public string Name{get; set;}
  public List<Child> Childs {get; set;}
}

public class Child
{
  public string Name{get; set;}
  public int Age {get; set;}
  public bool Married {get; set;}
}

public class ParentFactory
{
   public List<Parent> Parents {get; set;}

   public ParentFactory()
   {
      Child child1 = new Child() {Name="Peter", Age=10, Married=true};
      Child child2 = new Child() {Name="Mary", Age=9, Married=false};
      Child child3 = new Child() {Name="Becky", Age=12, Married=true};

      Parent parent1 = new Parent(){Name="Adam", Childs = new List<Child>(){child1, child2}};
      Parent parent2 = new Parent(){Name="Kevin", Childs = new List<Child>(){child3}};

      Parents = new List<Parent>(){parent1, parent2};
   }
}

I want to bind the object ParentFactory parentFactory = new ParentFactory() to ItemsControl:

<DockPanel>
    <ItemsControl ItemsSource="{Binding Parents}">
    </ItemsControl>
</DockPanel>

 <Window.Resources>
     <DataTemplate DataType="{x:Type Parent}">
         <StackPanel Margin="2,2,2,1">
              <Expander Header="{Binding Name}">
                  <ItemsControl ItemsSource="{Binding Childs}" />
              </Expander>
         </StackPanel>
     </DataTemplate>
        <DataTemplate DataType="{x:Type Child}">
            <StackPanel>
                    <TextBox Grid.Column="0" Text="{Binding Name}" />
                    <TextBox Grid.Column="1" Text="{Binding Age}"/>
                    <CheckBox Grid.Column="2" IsChecked="{Binding Married}"/>
            </StackPanel>
        </DataTemplate>
 </Window.Resources>

In the Stackpanel, there are two types of controls: TextBox and CheckBox. However, I want them to be more dynamic: if the value is a boolean then use a Checkbox and else use a Textbox. That means I dont need to define the control either TextBox or Checkbox inside the StackPanel due to a numerous attributes in my Child class. Would it be possible, and if yes, how can I achieve them?

4

3 回答 3

3

您可以DataTemplate动态更改

Xaml

                <DataTemplate>
                    <DataTemplate.Resources>
                        <DataTemplate x:Key="Condition1"></DataTemplate>
                        <DataTemplate x:Key="Condition2"></DataTemplate>
                    </DataTemplate.Resources>                    
                </DataTemplate>
                    <ContentPresenter x:Name="ContentField"
                                      Content="{Binding}"
                                      ContentTemplate="{StaticResource ResourceKey=Condition1}" /> 
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=IsMarried}" Value="True">
                            <Setter TargetName="ContentField" Property="ContentTemplate" Value="{StaticResource ResourceKey=Condition2}" />
                        </DataTrigger>
                    </DataTemplate.Triggers>   

确保正确设置绑定......并制作DataTemplatesforCondition1Condition2

希望能帮助到你 :)

于 2012-04-18T08:31:35.753 回答
2

我已经根据我从您的问题中了解到的情况做了一个解决方案。请看一看。该示例基于 DataTrigger,您可以将逻辑更改为 Converter。

<Window x:Class="StackAnswers.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:t="clr-namespace:StackAnswers">


<Window.Resources>
    <DataTemplate DataType="{x:Type t:Parent}">
        <StackPanel Margin="2,2,2,1">
            <Expander Header="{Binding Name}">
                <ItemsControl ItemsSource="{Binding Childs}" />
            </Expander>
        </StackPanel>
    </DataTemplate>
    <DataTemplate DataType="{x:Type t:Child}">
        <StackPanel>
            <TextBlock Text="{Binding Name}"></TextBlock>
            <TextBox Grid.Column="0"
                     Text="{Binding Name}">
                <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                        <Setter Property="Visibility"
                                Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Married}" Value="false">
                                <Setter Property="Visibility"
                                        Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>
            <TextBox Grid.Column="1"
                     Text="{Binding Age}">
                <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Visibility"
                            Value="Collapsed" />
                    <Style.Triggers>
                            <DataTrigger Binding="{Binding Married}"
                                     Value="false">
                            <Setter Property="Visibility"
                                    Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
                    </TextBox.Style>
            </TextBox>
            <CheckBox Grid.Column="2"
                      IsChecked="{Binding Married}" Content="Married">
                <CheckBox.Style>
                    <Style TargetType="{x:Type CheckBox}">
                        <Setter Property="Visibility"
                                Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Married}" Value="True">
                                <Setter Property="Visibility"
                                        Value="Visible"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </CheckBox.Style>
            </CheckBox>
        </StackPanel>
    </DataTemplate>


</Window.Resources>
<DockPanel>
    <ItemsControl ItemsSource="{Binding Parents}">
    </ItemsControl>
</DockPanel>

于 2012-04-18T08:58:00.660 回答
1

看看这篇文章:http ://www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx 我认为这是你需要的:

<Page
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <Page.Resources>
    <DataTemplate DataType="{x:Type sys:Boolean}">
      <CheckBox IsChecked="{Binding Mode=OneWay}" />
    </DataTemplate>
  </Page.Resources>
  <ItemsControl Width="100" Height="100">
    <sys:Int32>30</sys:Int32>
    <sys:DateTime>12/16/1970</sys:DateTime>
    <sys:Boolean>True</sys:Boolean>
    <sys:Boolean>False</sys:Boolean>
    <sys:String>Foo</sys:String>
  </ItemsControl>
</Page>
于 2012-04-18T08:18:02.047 回答