簡體   English   中英

更改另一個控件的值時如何執行Converter?

[英]How to execute Converter when value of the another control get's changed?

我有兩個ComboBox ComboBox1和ComboBox2,我想在ComboBox1 SelectedItem更改時執行ComboBox2轉換器,如何在XAML中完成。

到目前為止,我對於ComboBox2擁有以下XAML:

ItemsSource="{Binding MyItems, Converter={StaticResource MYConverter}, ConverterParameter= {Binding ElementName=comboBox1, Path=SelectedItem,Mode=TwoWay}}" 

僅當更改綁定值時才重新評估綁定,而不是轉換器參數,因此,請假定:

  1. SelectedItem1視圖模型的屬性綁定到ComboBox1小號SelectedItem屬性。

  2. SelectedItem2視圖模型的屬性綁定到ComboBox2小號SelectedItem屬性。

  3. ComboBox2的ViewModel的ItemSource屬性綁定到ViewModel上名為MyItems的屬性:

無論何時更改SelectedItem1 ,都應引發MyItems PropertyChanged事件。 這樣,重新評估綁定並執行轉換器。

PS:請在將來提供有關您的問題的更多背景信息,例如ViewModel的外觀。

在后面的代碼中,您可以創建如下代碼:

comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1SelectionChanged);

它將生成一個事件處理程序,該事件處理程序將由委托調用,該處理程序應如下所示:

void comboBox1SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //Your logic here
    }

不要忘記:

ü

sing System.Windows;
using System.Windows.Controls;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM