简体   繁体   中英

Getting value of another control in a converter

I have 2 radio buttons, bounded to an XML via XPath. If I select the first radio button, I will need to get the selected value of a combo box, and set it to a XML Element. However, if I select the second radio button, I'll just need to set a fixed hardcoded value.

I'm having trouble getting the value from the combo box when I select the first radio button. I've tried using ConverterParameter (and I found out it doesn't allow bindings), and using MultiBinding didn't help either.

Please advice.

Thanks!

You were on the right track with MultiBinding and MultiValueConverter. Here's a simple example of something similar to your needs:

<Window.Resources>
    <my:MyMultiValueConverter x:Key="MyMultiValueConverter" />
</Window.Resources>

<StackPanel Orientation="Vertical" >
    <RadioButton x:Name="FirstRadioButton" GroupName="MyButtonGroup">First</RadioButton>
    <RadioButton x:Name="SecondRadioButton" GroupName="MyButtonGroup">Second</RadioButton>
    <ComboBox x:Name="MyComboBox" SelectedValuePath="Content">
        <ComboBoxItem>First Item</ComboBoxItem>
        <ComboBoxItem>Second Item</ComboBoxItem>
    </ComboBox>

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                <Binding ElementName="FirstRadioButton" Path="IsChecked" />
                <Binding ElementName="MyComboBox" Path="SelectedValue" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</StackPanel>

and converter:

class MyMultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var firstRadioButtonValue = (bool)values[0];
        var comboBoxSelectedValue = values[1];

        return firstRadioButtonValue ? comboBoxSelectedValue : "MyStaticValue";
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Edit:

If you only need to react to the Checked event of RadioButton(s), then you can go with something like this:

<Window.DataContext>
    <my:MainWindowViewModel />
</Window.DataContext>

<StackPanel Orientation="Vertical">
    <RadioButton GroupName="MyButtonGroup" Content="First" IsChecked="True">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding PassSelectedValueToXmlCommand}" CommandParameter="{Binding SelectedValue, ElementName=MyComboBox}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </RadioButton>
    <RadioButton GroupName="MyButtonGroup" Content="Second">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding PassSelectedValueToXmlCommand}" CommandParameter="My static value" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </RadioButton>
    <ComboBox x:Name="MyComboBox" SelectedValuePath="Content">
        <ComboBoxItem Content="First item" IsSelected="True" />
        <ComboBoxItem Content="Second Item" />
    </ComboBox>
</StackPanel>

Note: you need to reference System.Windows.Interactivity and use it in the Window/Page/Control... xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

You can then handle this in the ViewModel:

    public MainWindowViewModel()
    {
        PassSelectedValueToXmlCommand = new DelegateCommand<string>(HandleCommand);
    }

    public ICommand PassSelectedValueToXmlCommand { get; set; }

    private void HandleCommand(string parameter)
    {
        MessageBox.Show(String.Format("Parameter with value {0} was received by ViewModel", parameter));
    }

You don't even need converter for such case, but you won't handle ComboBox selection changed that way. If you need this as well, then you can "save" the bound value(s) to hidden TextBlock.Tag or StackPanel's Tag or something similar. I'm sure you can figure it out now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM