WPF将绑定传递给我的用户控件

时间:2015-05-07 18:44:52

标签: c# wpf xaml

我创建了一个具有Value属性的简单UserControl:

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(""));
}

我正在使用这个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>

当我像这样设置值明确时,这是有效的:

<uc:Label Value="Name" />

但是出于某种原因,当我尝试使用绑定时它并没有:

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

我的userControl的Value属性未被更新,我收到了绑定错误: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')

这是object它正在寻找房产,我做错了什么?

2 个答案:

答案 0 :(得分:4)

DataContext是从父级继承的,它会清除您的手动设置器。

您可以通过更改XAML以将内容的DataContext绑定到控件本身,然后绑定将继续工作,因为继承的上下文始终是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>

注意我已使用x:Name="Root"为控件命名,并将Border上下文绑定为DataContext="{Binding ElementName=Root}"

您可以从后面的代码中删除DataContext = this

答案 1 :(得分:1)

这个问题可能与DP有关吗? 如果您将Label重命名为其他内容,它是否会解决问题? 此外,您可能不需要重写数据上下文...它应该继承 来自其父控件,因为您将父属性绑定到它。

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(""));
}