简体   繁体   中英

Wpf change property action from xaml

Is it possible to change state from 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

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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