积极答复者
|ZYCWPF| 如要在DataTrigger中去修改ViewModel的MyMessage属性 有示例代码 谢谢

问题
-
<Window x:Class="SRQC_DataTrigger_Property.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox x:Name="txt1" /> <TextBox x:Name="txt2" /> <TextBox x:Name="txt3" Text="{Binding MyMessage}" /> </Grid> </Window>
在XAML中写触发器,如何写当txt1.Text="ABC"和txt2.Text="123"的时候,设置MyMessage="Changed";
答案
-
这样做:
<StackPanel> <TextBox x:Name="txt1" /> <TextBox x:Name="txt2" /> <TextBox x:Name="txt3" > <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Text" Value="{Binding MyMessage}" /> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding ElementName=txt1, Path=Text}" Value="ABC" /> <Condition Binding="{Binding ElementName=txt2, Path=Text}" Value="123" /> </MultiDataTrigger.Conditions> <Setter Property="Text" Value="Changed" /> </MultiDataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> </StackPanel>
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Sheldon _XiaoModerator 2012年11月6日 8:09
全部回复
-
这个是CS
using System.ComponentModel; using System.Windows; namespace SRQC_DataTrigger_Property { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowViewModel(); } class MainWindowViewModel : NotificationObject { private string myMessage; /// <summary> /// ?? /// </summary> public string MyMessage { get { return myMessage; } set { myMessage = value; this.RaisePropertyChanged("MyMessage"); } } } class NotificationObject : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } } }
-
这样做:
<StackPanel> <TextBox x:Name="txt1" /> <TextBox x:Name="txt2" /> <TextBox x:Name="txt3" > <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Text" Value="{Binding MyMessage}" /> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding ElementName=txt1, Path=Text}" Value="ABC" /> <Condition Binding="{Binding ElementName=txt2, Path=Text}" Value="123" /> </MultiDataTrigger.Conditions> <Setter Property="Text" Value="Changed" /> </MultiDataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> </StackPanel>
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Sheldon _XiaoModerator 2012年11月6日 8:09