【问题标题】:How to change the controle depending upon the data type in WPF如何根据 WPF 中的数据类型更改控件
【发布时间】:2013-02-01 04:45:51
【问题描述】:

我必须使用 WCF 服务从 DB 中检索值,并使用 ID-Type 值填充名为“Type”的下拉列表(使用 observable 集合来绑定它)。

应该有另一个由数据模板/控件模板控制的控件,它将根据选择的类型显示。例如如果选择了 TextBox 类型,则 TextBox 应该显示一些默认值。

InputType 文本框 - 这将用于在 DB 中创建新类型。使用保存按钮保存值。

删除按钮 - 这应该从数据库中删除选定的类型。

我已经完成了 DataBase Stuff 和所有内容,但是我应该如何根据 XAML 中的数据类型更改控件?

【问题讨论】:

    标签: wpf datatemplate


    【解决方案1】:

    您可以使用具有样式的通用 ContentControl,该样式将选择(通过触发器)包含适当控件类型的不同 ControlTemplate。

    此方法也可以稍作修改以使用 DataTemplates 而不是 ControlTemplates(可以说是更好的方法)。不要设置 Template 属性(它是一个 ControlTemplate),而是设置 ContentTemplate 属性(它是一个 DataTemplate)并用您想要的控件填充每个 DataTemplate。

    <Window x:Class="ControlTypeBasedOnComboBox.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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0"
                  ItemsSource="{Binding Path=ControlTypes}"
                  x:Name="ControlTypeComboBox"/>
        <ContentControl Grid.Row="1">
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=ControlTypeComboBox, Path=SelectedItem}" Value="TextBox">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ContentControl">
                                        <TextBox/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=ControlTypeComboBox, Path=SelectedItem}" Value="CheckBox">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ContentControl">
                                        <CheckBox/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=ControlTypeComboBox, Path=SelectedItem}" Value="Button">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ContentControl">
                                        <Button/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </Grid>
    

    代码隐藏:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }
    

    视图模型:

    public class ViewModel
    {
        ObservableCollection<string> controlTypes;
        public ViewModel()
        {
            controlTypes = new ObservableCollection<string>() { "TextBox", "CheckBox", "Button" };
        }
    
        public ObservableCollection<string> ControlTypes
        {
            get { return controlTypes; }
        }
    }    
    

    至于保存/删除按钮,您还可以根据 ComboBox 的 SelectedItem 将 Command 属性绑定到 View Model 上的不同 ICommand 对象。我不确切知道您需要什么样的功能,所以我不知道这是否必要/合适。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      相关资源
      最近更新 更多