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

WPF Binding - Bind to A when X is checked, else bind to B

基本上我有一个显示麦克风增益的 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"

是否可能,或者我是否必须将两个增益变量合并在一起?

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

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

<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 参数,以便它知道应该 return.

哪个枚举的值

以上示例无需任何修改即可运行。