使用文本框或复选框取决于一个attribue的类型 [英] using textbox or checkbox depending on the type of an attribue

查看:142
本文介绍了使用文本框或复选框取决于一个attribue的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的结构:

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 =新ParentFactory();到ItemsControl的:

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>

在的StackPanel,有两种类型的控制:文本框和CheckBox。不过,我想他们更动态的:如果该值是一个布尔然后使用复选框,并可以使用一个文本框。这意味着我不需要定义控制或者文本框或StackPanel的内部复选框由于我的孩子一类众多的属性。可能吗?如果是,我怎么能实现这些目标。请您提供一些代码示例?

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. could you please provide some code samples?

在此先感谢。

推荐答案

我已经做了从我从你的问题理解的解决方案。请看看吧。
样品是基于DataTrigger,你可以改变的逻辑,以转换

I have done a solution from what i understood from your question. Please have a look at it. The sample is based on the DataTrigger and you can change the logics to 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>



这篇关于使用文本框或复选框取决于一个attribue的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆