是否有可能从xaml更改状态?例如,当第一个按钮启动时,使用"State“值字符串或bool。需要写的东西。目标名称应该是什么?
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:ChangePropertyAction TargetName="State" Value="False"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Content="Button" HorizontalAlignment="Left" Margin="94,10,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>代码:
namespace WpfApplication114
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new Data();
}
}
public class Data
{
private string state;
public string State
{
get { return this.state; }
set { this.state = value; }
}
}}
发布于 2015-08-05 09:12:04
嗨,如果我没有误解你的问题,你可以这样做
TriggerAction类
public class TriggerActionClick : TriggerAction<Button>
{
public string Value { get; set; }
public string TargetName { get; set; }
protected override void Invoke(object parameter)
{
if (AssociatedObject.DataContext != null && !string.IsNullOrEmpty(TargetName))
{
var type=AssociatedObject.DataContext.GetType();
var prop = type.GetProperty(TargetName);
if (prop != null)
prop.SetValue(AssociatedObject.DataContext,Value);
}
}
}在这里,当您单击Button时,xaml中的值将被设置为DataContext对象的DataContext中指定的属性。
https://stackoverflow.com/questions/31827587
复制相似问题