2012-04-18 59 views
4

如果我有這種結構的類型:文本框或複選框取決於屬性

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}; 
    } 
} 

我想將對象ParentFactory parentFactory = new ParentFactory()結合的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> 

在StackPanel中,有兩種類型的控件:TextBox和CheckBox。但是,我希望它們更具動態性:如果值是布爾值,則使用複選框,否則使用文本框。這意味着我不需要在我的Child類中定義多個屬性來控制StackPanel中的TextBox或Checkbox。這是可能的,如果是的話,我該如何實現它們?

+1

你的意思是,如果布爾= true,則複選框或文本框..?如果是這種情況,那麼你可以使用DataTemplate中的Triggers – Ankesh 2012-04-18 08:08:25

+0

是的,一個屬性(已婚)是一個布爾值,然後使用複選框。否則使用文本框。 – olidev 2012-04-18 08:09:33

+0

@ adcool2007你能否提供如何在Datatemplate中使用觸發器的示例代碼?謝謝 – olidev 2012-04-18 08:11:21

回答

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> 

+0

庫馬爾:你太棒了!這就像我問的那樣。但是,我不清楚我的問題。我只是在這裏打開了另一個問題:http://stackoverflow.com/questions/10206718/using-either-checkbox-or-textbox-for-an-enum-type我覺得這個更具挑戰性。如果你有時間,請幫我再來一次?非常感謝! – olidev 2012-04-18 09:38:59

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> 
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> 

一定要設置綁定正確...並使DataTemplatesCondition1Condition2

希望它能幫助:)