简体   繁体   中英

How to add a Property/Attribute to UserControl with no Code behind?

First up, is it possible to add a property to a WPF UserControl with no code behind?

If not, lets say I have a custom UserControl like this:

<UserControl x:Class="Example.Views.View"
         xmlns:vm ="clr-Example.ViewModels"
         xmlns:view ="clr-Example.Views"
         ... >
   <UserControl.DataContext>
     <vm:ViewModel/>
   </UserControl.DataContext>

   <Button Background="Transparent" Command="{Binding ClickAction}">
     <Grid>
        ...
        <Label Content="{Binding Description}"/>
     </Grid>
   </Button>
</UserControl>

With The ViewModel like this

public class ViewModel : INotifyPropertyChanged
{

    private ICommand _clickAction;
    public ICommand ClickAction
    {
        get { return _clickAction; }
        set
        {
            if (_clickAction != value)
            {
                _clickAction = value;
                RaisePropertyChanged("ClickAction");
            };
        }
    }

    private int _description;
    public int Description
    {
        get { return _description; }
        set
        {
            if (_description!= value)
            {
                _description = value;
                RaisePropertyChanged("Description");
            };
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        // take a copy to prevent thread issues
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }

I want to be able to set the Action like this:

...
<UserControl.Resources>
    <ResourceDictionary>
        <command:ButtonGotClicked x:Key="gotClicked" />
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <view:FuelDispenserView  ClickAction="{StaticResource gotClicked}"/>
</Grid> ...

Without Code behind.

Currently I use this ugly code to achive my goal but I don't like it.

public partial class View : UserControl
{
    public View()
    {
        InitializeComponent();
    }
    public ICommand ClickAction {
        get {
            return ((ViewModel)(this.DataContext)).ClickAction;
        }
        set {
            ((ViewModel)(this.DataContext)).ClickAction = value;
        }
    }
}

Does anybody have a better Idea how to do this?

PS This is not just meant for this Action. I have different Properties I need to add.

You can use attached properties logic to add custom properties to your user control, but It looks like you have to define different behavior for ClickAction in different views, so I'm not sure it would be useful for you. I suggest you to use routed command and command bindings - it may be helpful in this case.

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