Specify Binding of DependencyProperty in UserControl itself

不打扰是莪最后的温柔 提交于 2019-12-24 12:23:07

问题


Following up on my previous question (Change brushes based on ViewModel property)

In my UserControl I have have a DependencyObject. I want to bind that object to a property of my ViewModel. In this case a CarViewModel, property name is Status and returns an enum value.

public partial class CarView : UserControl
{
    public CarStatus Status
    {
        get { return (CarStatus)GetValue(CarStatusProperty); }
        set { SetValue(CarStatusProperty, value); }
    }

    public static readonly DependencyProperty CarStatusProperty =
        DependencyProperty.Register("Status", typeof(CarStatus), typeof(CarView), new PropertyMetadata(OnStatusChanged));

    private static void OnStatusChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        var control = (CarView)obj;
        control.LoadThemeResources((CarStatus)e.NewValue == CarStatus.Sold);
    }

    public void LoadThemeResources(bool isSold)
    {
        // change some brushes
    }
}


<UserControl x:Class="MySolution.Views.CarView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:views="clr-MySolution.Views"
             mc:Ignorable="d"
             views:CarView.Status="{Binding Status}">   
    <UserControl.Resources>
    </UserControl.Resources>
    <Grid>
        <TextBlock Text="{Binding Brand}"FontSize="22" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
<UserControl

Where do I need to specify this binding? In the root of the UserControl it gives an error:

The attachable property 'Status' was not found in type 'CarView'

In my MainWindow I bind the CarView using a ContentControl:

<ContentControl
    Content="{Binding CurrentCar}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type viewmodel:CarViewModel}">
            <views:CarView />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

My ViewModel:

[ImplementPropertyChanged]
public class CarViewModel
{
    public Car Car { get; private set; }

    public CarStatus Status
    {
        get
        {
            if (_sold) return CarStatus.Sold;
            return CarStatus.NotSold;
        }
    }
}

回答1:


your binding isn't well written. instead of writing views:CarView.Status="{Binding Status}" you should write only Status="{Binding Status}"




回答2:


It seems that your Control is binding to itself.
Status is looked for in CarView.

You should have a line of code in your control CodeBehind like :

this.DataContext = new ViewModelObjectWithStatusPropertyToBindFrom();

Regards



来源:https://stackoverflow.com/questions/33076560/specify-binding-of-dependencyproperty-in-usercontrol-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!