是否可以从xaml更改状态?例如,当第一个按钮触发“状态”时,一些值字符串或布尔值。需要写些什么。目标名称应该是什么?
<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; }
}
}
}
嗨,如果我没有误解您的问题,您可以这样做
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对象的TargetName中指定的属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句