如何有条件地绑定数据?

发布于 2024-09-29 01:27:39 字数 168 浏览 1 评论 0原文

如何有条件地将数据绑定到组合框?我有一个组合框,默认情况下应显示 ID。然而,如果用户选中复选框,则显示屏应同时显示 ID 和 NAME。例如,“OO1:山姆”。我的默认 ID 显示正确。我只是不确定如何根据复选框的“IsChecked”状态显示 ID 和 NAME。

WPF 和 C# .Net 3.5

How do I conditionally bind data to a combo box? I have a combo box that by default should display an ID. However, if the user checks a check box then the display should display both the ID and a NAME. For example, "OO1: Sam". I have the default ID displaying correctly. I'm just not sure how to get both the ID and the NAME to display based on the "IsChecked" state of the check box.

WPF and C# .Net 3.5

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

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

发布评论

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

评论(2

少女的英雄梦 2024-10-06 01:27:39

这是一种方法,使用带有触发器的 ComboBox 上的样式来动态设置 ItemTemplate:

编辑:将样式更改为资源。请注意,这仍然是使用元素绑定直接绑定到 CheckBox - 如果您希望它更灵活,您可以将 CheckBox 的 IsChecked 属性绑定到 ViewModel 的属性,并依赖于该更改而不是 IsChecked。

让我们将样式移动到窗口的资源部分:

<Window.Resources>
    <Style x:Key="myStyle" TargetType="ComboBox">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked,ElementName=chk}" Value="True">
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding ID}" />
                                <TextBlock Text=": " />
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

现在我们定义复选框和几个依赖它的组合框:

<CheckBox x:Name="chk" Content="Click Me" />

<ComboBox ItemsSource="{Binding}" Style="{StaticResource myStyle}" />

<ComboBox ItemsSource="{Binding}" Style="{StaticResource myStyle}" />

Here's one way, using a style on the ComboBox with triggers to set the ItemTemplate dynamically:

Edit: Changing the style into a resource. Note that this is still binding to the CheckBox directly using element binding - if you want it to be more flexible you could bind the CheckBox's IsChecked property to a property of your ViewModel and rely on that changing rather than IsChecked.

Let's move the style into the Resources section of our Window:

<Window.Resources>
    <Style x:Key="myStyle" TargetType="ComboBox">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked,ElementName=chk}" Value="True">
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding ID}" />
                                <TextBlock Text=": " />
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

Now we define the CheckBox and a couple of ComboBoxes that rely on it:

<CheckBox x:Name="chk" Content="Click Me" />

<ComboBox ItemsSource="{Binding}" Style="{StaticResource myStyle}" />

<ComboBox ItemsSource="{Binding}" Style="{StaticResource myStyle}" />
病毒体 2024-10-06 01:27:39

我会在 ViewModel 中这样做。您可以为组合框项目创建一个视图模型,为复选框所在的任何屏幕创建另一个视图模型,并通过某种方式让复选框视图模型在其值发生更改时告诉项目视图模型。然后,项目视图模型在其 Text 属性(或任何您所称的名称)中具有条件逻辑,并实现通常的 INotifyPropertyChanged 模式以在其文本发生更改时通知 UI。

好处:这样您就可以为此行为编写单元测试。 (如果值得投入,就值得为其编写单元测试。)

I would do that in the ViewModel. You could have a viewmodel for your combo box items, another one for whatever screen the checkbox lives on, and some way for the checkbox viewmodel to tell the item viewmodels when its value has changed. The item viewmodel then has conditional logic in its Text property (or whatever you call it), and implements the usual INotifyPropertyChanged pattern to notify the UI when its text has changed.

The benefit: this way you can write unit tests for this behavior. (And if it's worth putting in, it's worth writing unit tests for.)

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