WPF绑定 - 当X被选中时绑定到A,否则绑定到B

3
基本上,我有一个 TextBlock 显示麦克风增益。
<TextBlock FontFamily="Calibri Light" FontSize="20" Foreground="#FFF65B60" FontWeight="Bold" Height="35"><Run Text="{Binding AudioRecorder.Gain, StringFormat={}Microphone Gain: {0:#} %}"/></TextBlock>

正如您所见,这是绑定到“AudioRecorder.Gain”的,但如果未选中此复选框,则我只想绑定到该值。

<CheckBox IsChecked="{Binding Recognizer.AutoGainControl}"

如果它被选中,我想要绑定到 "Recognizer.Gain"。这样的事情可能吗?还是我必须将两个增益变量合并在一起?

你正在寻找 WPF 中的条件绑定。请查看此 SO 帖子,了解如何实现它。 - Michael
谢谢!如果你不知道关键词,那么在谷歌上很难搜索到 :) 这正是我所需要的。我该如何接受你的评论作为答案? - user2542913
你不能把评论作为答案接受,我这里不是在寻找积分,很高兴能够帮到你! - Michael
Michael是绝对正确的,你当然可以这样做。但是我建议,仅仅因为你可以在WPF中做某些事情,并不意味着你应该这样做。MVVM的整个重点在于逻辑在你的(可测试的)视图模型中而不是视图中。在这种情况下,你正在将逻辑放入视图中,而且它是无法测试的。如果是我的话,我会在那里创建一个单一的有意义的数据,表达视图中所代表的内容,无论是什么......你在视图中看到的必须代表对用户有意义的某些东西,无论状态如何。 - karfus
我不使用MVVM。MVVM是一个很好的模式,但不适用于我的情况。 - user2542913
我的错误,我猜我只是假设你是。我很好奇WPF有什么更好的UI模式? - karfus
1个回答

3

我不确定你是否成功了,但是一些例子应该留在这里供其他可能搜索相同内容的人参考:

我已经收集了一些信息,并创建了一个版本:

<Window x:Class="ComboItems.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:windows="clr-namespace:System.Windows;assembly=PresentationCore"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Width="525">
<Window.Resources>
    <x:Array x:Key="data1" Type="{x:Type system:String}">
        <system:String>Item1</system:String>
        <system:String>Item2</system:String>
        <system:String>Item3</system:String>
    </x:Array>
    <ObjectDataProvider x:Key="visibilityValues" 
                            ObjectType="{x:Type system:Enum}"
                            MethodName="GetValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="windows:Visibility" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<Grid>
      <StackPanel>
        <RadioButton Content="RadioButton1" Name="Radio1" GroupName="radio"  />
        <RadioButton Content="RadioButton2" Name="Radio2"   GroupName="radio"  />
        <ListBox Name="listbox">
            <ListBox.Style>
                <Style TargetType="ListBox">
                    <Setter Property="ItemsSource">
                        <Setter.Value>
                            <Binding Source="{StaticResource data1}" />
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=Radio1}" Value="True" >
                            <Setter Property="ItemsSource">
                                <Setter.Value>
                                    <Binding Source="{StaticResource data1}" />
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=Radio2}" Value="True" >
                            <Setter Property="ItemsSource">
                                <Setter.Value>
                                    <Binding Source="{StaticResource visibilityValues}" />
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Style>
        </ListBox>
    </StackPanel>
  </Grid>
</Window>

DataTrigger将在此处发挥作用,并根据两个RadioButtonIsChecked属性更改ListBox的源。

此外,我已经使用绑定来枚举帮助System.Enum类型的GetValues方法,该方法接受一个Type参数,以便知道它应返回哪个枚举的值。

上述示例应该可以正常工作,无需进行任何修改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接