![](/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.