WPF binding - binding to A when checking X and also binding to B

Basically I have a TextBlock that displays the microphone gain.

<TextBlock FontFamily="Calibri Light" FontSize="20" Foreground="#FFF65B60" FontWeight="Bold" Height="35"><Run Text="{Binding AudioRecorder.Gain, StringFormat={}Microphone Gain: {0:#} %}"/></TextBlock>

      

As you can see it is related to "AudioRecorder.Gain", however I only want to bind to this value if this checkbox is NOT checked.

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

      

if it is installed i want to bind to

"Recognizer.Gain"

Is something like this possible or do I need to combine two variable gains together?

+3


source to share


1 answer


I'm not sure if you succeeded or not, but some example should stay here for others who may be looking for the same thing:

I gathered some information and created a version of this:

<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

will do the job here and according to the property IsChecked

how RadioButton

it will change the source ListBox

.

Also, I used to bind to enums with the System.Enum

types method GetValues

, which accepts Type

so that it knows what enum values ​​it should return.

The above sample should work without any modification.

+3


source







All Articles