![](/img/trans.png)
[英]How to execute Converter when value of the another control get's changed?
[英]Getting value of another control in a converter
我有2个单选按钮,它们通过XPath绑定到XML。 如果选择第一个单选按钮,则需要获取组合框的选定值,并将其设置为XML元素。 但是,如果选择第二个单选按钮,则只需要设置一个固定的硬编码值即可。
选择第一个单选按钮时,无法从组合框中获取值。 我试过使用ConverterParameter(我发现它不允许绑定),并且使用MultiBinding也没有帮助。
请指教。
谢谢!
使用MultiBinding和MultiValueConverter,您走在正确的轨道上。 这是满足您需求的简单示例:
<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>
和转换器:
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();
}
}
编辑:
如果您只需要对RadioButton的Checked事件做出反应,则可以执行以下操作:
<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>
注意:您需要引用System.Windows.Interactivity并在Window / Page / Control中使用它。xmlns:i =“ http://schemas.microsoft.com/expression/2010/interactivity”
然后,您可以在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));
}
在这种情况下,您甚至不需要转换器,但是您将无法处理以这种方式更改的ComboBox选择。 如果还需要此功能,则可以将绑定值“保存”到隐藏的TextBlock.Tag或StackPanel的Tag或类似的东西。 我确定您现在可以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.