根据用户对下拉列表的选择显示控件

发布于 2024-12-02 21:12:07 字数 163 浏览 0 评论 0原文

我需要根据用户在下拉列表中所做的选择来显示不同的控件。也就是说,如果用户对日期进行选择,则应显示日期选择控件,或用于文本输入的文本输入框......或者用于更复杂选择的一组控件。

因此,我们的想法是创建任意数量的用户控件,然后在需要的时间显示所需的控件。我想要比“可见的真/假”属性更优雅的东西。

I need to display different controls depending on what selection a user makes in a drop down. That is, if the user makes a selection for a date, a date selection control should display, or text input box for text input... Or a set of controls for more complex selections.

So the idea is to create how ever many user controls, and then display the required one at the required time. I want something more elegant than faffing with 'visible true/false' properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-12-09 21:12:07

您可以使用内容控制和数据触发器来实现它。无需玩弄可见性。

这是一个示例,可以让您清楚地了解。
在下面的示例中,我采用了一个内容控件,其中组合框所选项目作为数据上下文。
XAML 代码:

 <StackPanel Orientation="Vertical" Width="150">
        <ComboBox Name="Controls"></ComboBox>
        <ContentControl DataContext="{Binding ElementName=Controls,Path=SelectedItem}">
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding}" Value="Date">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <DatePicker Height="30"></DatePicker>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding}" Value="Text">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <TextBox Height="30"></TextBox>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding}" Value="Button">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <Button Height="30"></Button>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding}" Value="Checkbox">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <CheckBox Height="30"></CheckBox>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </StackPanel>

代码隐藏:

InitializeComponent();
            List<string> controlTypes = new List<string> { "Date", "Text", "Button", "Checkbox" };
            Controls.ItemsSource = controlTypes;

注意:为了创建示例,我使用了代码隐藏...您可以轻松地将其转换为 MVVM

You can achieve it using content control and data triggers. No need to play with Visibility.

Here is a sample which will give you clear idea.
In the below sample i have taken a content control with combo box selected item as datacontext.
XAML Code:

 <StackPanel Orientation="Vertical" Width="150">
        <ComboBox Name="Controls"></ComboBox>
        <ContentControl DataContext="{Binding ElementName=Controls,Path=SelectedItem}">
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding}" Value="Date">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <DatePicker Height="30"></DatePicker>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding}" Value="Text">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <TextBox Height="30"></TextBox>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding}" Value="Button">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <Button Height="30"></Button>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding}" Value="Checkbox">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <CheckBox Height="30"></CheckBox>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </StackPanel>

Code Behind:

InitializeComponent();
            List<string> controlTypes = new List<string> { "Date", "Text", "Button", "Checkbox" };
            Controls.ItemsSource = controlTypes;

Note: For creating a sample i have used code behind... you can easily convert it to MVVM

燕归巢 2024-12-09 21:12:07

我想要比“可见真/假”属性更优雅的东西。

:-) 无论如何,你最终都会这样做......

这太微不足道了......任何像样的 WPF 相关教程都会帮助你......

只是为了快速开始,你将不得不使用 Binding ComboBox 的 SelectedItem 以及您想要通过适当的值转换器隐藏/显示的所有控件的 Visibility

I want something more elegant than faffing with 'visible true/false' properties.

:-) You will land up doing exactly that anyways....

This is too trivial ... any decent WPF related tutorial will help you with this...

Just for a quick start, you will have to use Binding of ComboBox's SelectedItem with Visibility of all those controls you want to hide/show through a proper value converter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文