WPF passes binding to my User Control

I created a simple UserControl with a property Value

:

public partial class Label : UserControl
{
    public Label()
    {            
        InitializeComponent();
        DataContext = this;
    }

    public object Value
    {
        get { return GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(Label), new PropertyMetadata(""));
}

      

And I am using this XAML:

<Border BorderBrush="#A0A0A0" BorderThickness="1, 1, 0, 0" CornerRadius="1">
    <Border BorderBrush="#000000" BorderThickness="0, 0, 1, 1" CornerRadius="1">
        <Border.Background>
            <LinearGradientBrush StartPoint="0.5, 0" EndPoint="0.5, 1">
                <GradientStop Color="#FFFFFF" Offset="0.0" />
                <GradientStop Color="#E0E0E0" Offset="1.0" />
            </LinearGradientBrush>
        </Border.Background>
        <TextBlock Width="100" FontWeight="SemiBold" Padding="2" Text="{Binding Value}" />
    </Border>
</Border>

      

It works when I set the explicity value like this:

<uc:Label Value="Name" />

      

But for some reason this doesn't happen when I try to use the binding:

<ItemsControl ItemsSource="{Binding InfoDetran}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <!-- Doesn't work -->
                <uc:Label Value="{Binding Label}" />
                <!-- But this works -->
                <TextBox Text="{Binding Label}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

      

Value

My userControl property is not updating and I get a binding error:BindingExpression path error: 'Label' property not found on 'object' ''Label' (Name='')'. BindingExpression:Path=Label; DataItem='Label' (Name=''); target element is 'Label' (Name=''); target property is 'Value' (type 'Object')

What is this object

, it is looking for a property and what am I doing wrong?

+3


source to share


2 answers


DataContext

inherits from the parent that your installer destroys.

You can fix this by changing the XAML to bind the content DataContext

to the control itself, then the bindings will continue to work since the inherited context is always itself UserControl

:

<UserControl ...
             x:Name="Root">
    <Border DataContext="{Binding ElementName=Root}"  BorderBrush="#A0A0A0" BorderThickness="1, 1, 0, 0" CornerRadius="1">
        <Border BorderBrush="#000000" BorderThickness="0, 0, 1, 1" CornerRadius="1">
            <Border.Background>
                <LinearGradientBrush StartPoint="0.5, 0" EndPoint="0.5, 1">
                    <GradientStop Color="#FFFFFF" Offset="0.0" />
                    <GradientStop Color="#E0E0E0" Offset="1.0" />
                </LinearGradientBrush>
            </Border.Background>
            <TextBlock Width="100" FontWeight="SemiBold" Padding="2" Text="{Binding Value}" />
        </Border>
    </Border>
</UserControl>

      



Note. I named the control with x:Name="Root"

and bound the context Border

as DataContext="{Binding ElementName=Root}"

.

You can remove DataContext = this

from the code behind.

+4


source


Could DP be the problem? If you rename the shortcut to something else, will fix the problem? Also, you probably don't need to rewrite the data context ... it should inherit from its parent control since you are binding the parent property to it.



public partial class CustomLabel : UserControl
{
    public CustomLabel ()
    {            
        InitializeComponent();
    }

    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(CustomLabel), new PropertyMetadata(""));
}

      

+1


source







All Articles