使用WPF根据绑定属性动态显示控件 [英] Dynamically display a control depending on bound property using WPF

查看:59
本文介绍了使用WPF根据绑定属性动态显示控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性,它是数据库数据类型( char DateTime int float 等...),我想更改用于输入所选类型的值的控件.因此,对于文本值,我想要一个 TextBox ;对于日期值,我想要一个 DatePicker .

I've got a property which is a database data type (char, DateTime, int, float etc...) and I want to change the control used to enter a value of the selected type. So for text values I want a TextBox and for date values I want a DatePicker.

我想到的一种方法是在窗体上使用每个控件之一,并使用适当的 IValueConverter 实现设置其 Visibility .我知道这可以解决问题,但是会创建很多代码,感觉不太好.

One way I thought about doing it was to have one of each control on my form and set their Visibility using an appropriate IValueConverter implementation. I know this will work, but it would create a lot of code and doesn't feel very nice.

我想的另一种方式是使用 ContentPresenter 并使用 Style DataTriggers 设置其内容,但我无法获得它工作.

The other way I thought was to use a ContentPresenter and set its content with a Style and DataTriggers but I can't get it to work.

<Style x:Key="TypedValueHelper" TargetType="{x:Type ContentPresenter}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=DataType}" Value="Char">
            <Setter Property="Content" Value="???"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=DataType}" Value="Date">
            <Setter Property="Content" Value="???"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=DataType}" Value="Integer">
            <Setter Property="Content" Value="???"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

如果任何人都可以填写我的"???" 或提供更好的解决方案,请这样做.

If anyone can fill in my "???" or offer a better solution please do.

推荐答案

您可以将样式与setter和DataTemplates结合使用.尽管我不认为 ContentPresenter 是样式的正确控件,但由于它没有模板,因此基本上可以在代码中找到它的起点.

You could do a combination of style with setters and DataTemplates. You basically have the start for it in your code, although I don't think ContentPresenter is the right control to style, since it does not have a template.

创建这样的样式:

<Style x:Key="TypedValueHelper" TargetType="{x:Type ContentControl}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=DataType}" Value="Char">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=.}" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=DataType}" Value="Integer">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Slider Maximum="100" Minimum="0" Value="{Binding Path=.}"
                                         Orientation="Horizontal" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

然后使用 ContentControl 中的样式:

<ContentControl Content="{Binding MyValue}"
                        Style="{StaticResource TypedValueHelper}">

这篇关于使用WPF根据绑定属性动态显示控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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