简体   繁体   English

WPF从XAML更改属性操作

[英]Wpf change property action from xaml

Is it possible to change state from xaml? 是否可以从xaml更改状态? For example when first button firing assigh "State" some value string or bool. 例如,当第一个按钮触发“状态”时,一些值字符串或布尔值。 What need to write in . 需要写些什么。 What should be in target name? 目标名称应该是什么?

    <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>

Code: 码:

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; }
    }
}

} }

Hi If I have not misunderstood your question you can do it like this 嗨,如果我没有误解您的问题,您可以这样做

TriggerAction class 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);
        }

    }
}

Here when you click the Button the Value set in xaml will be set to the Property specified in TargetName of the DataContext object. 在这里,当您单击Button时,在xaml中设置的值将设置为DataContext对象的TargetName中指定的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM