使用复选框或文本框作为枚举类型

时间:2012-04-18 09:37:31

标签: c# wpf

如果我有这种结构:

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

public class Child
{
  public string Name{get; set;}
  public string Value{get; set;}
  public enum ValueType {get; set;}
}

public enum ValueType
{
    Int,
    Boolean,
    String
};

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

   public ParentFactory()
   {
      Child child1 = new Child() {Name="Filter1", Value="1", ValueType=ValueType.String};
      Child child2 = new Child() {Name="isExecuted", Value="true", ValueType=ValueType.Boolean};
      Child child3 = new Child() {Name="Width", Value="10", ValueType=ValueType.Int};

      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 Value}"/>
                    <TextBox Grid.Column="2" Text="{Binding ValueType}"/>
            </StackPanel>
        </DataTemplate>
 </Window.Resources>

在Stackpanel中,有一个控件:TextBox。但是,我希望它更具动态性:如果ValueType是布尔值,则使用复选框,然后使用文本框:<TextBox Grid.Column="1" Text="{Binding Value}"/>

这是否可能,如果可行,我该如何实现呢?

2 个答案:

答案 0 :(得分:2)

您可以动态更改DataTemplate

的Xaml

                <DataTemplate>
                    <DataTemplate.Resources>
                        <DataTemplate x:Key="Condition1">
                           <TextBox></TextBox> // Do you binding
                        </DataTemplate>
                        <DataTemplate x:Key="Condition2">
                           <CheckBox></CheckBox> // Do you binding
                        </DataTemplate>
                    </DataTemplate.Resources>                    
                </DataTemplate>
                    <ContentPresenter x:Name="ContentField"
                                      Content="{Binding}"
                                      ContentTemplate="{StaticResource ResourceKey=Condition1}" /> 
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=ValueType}" Value="1">
                            <Setter TargetName="ContentField" Property="ContentTemplate" Value="{StaticResource ResourceKey=Condition2}" />
                        </DataTrigger>
                    </DataTemplate.Triggers>   

务必正确设置绑定...并为DataTemplatesCondition1

制作Condition2

希望它有所帮助:)

答案 1 :(得分:1)

我认为最简单的方法是在面板中同时使用它们并定义两个新的布尔属性,通过布尔到可见性Visibility来控制它们ValueConverter

public bool IsValueTypeBoolean
{
    get
    {
         ...Conditions that will return true for CheckBox.
    }
}

public bool IsValueTypeOther
{
    get
    {
         return !this.IsValueBoolean;
    }
}

<TextBox Grid.Column="2" Visibility="{Binding IsValueTypeOther, Converter={StaticResource visibilityConverter}}"/> 
<CheckBox Grid.Column="2" Visibility="{Binding IsValueTypeBoolean, Converter={StaticResource visibilityConverter}}"/> 

布尔到VisibilityConverter

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool myValue = (bool)value;

        if (myValue)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

将此作为资源添加到您的XAML:

 <local:BooleanToVisibilityConverter x:Key="visibilityConverter"></local:VisibilityConverter>

我希望没有错别字......