0

这可能是一个非常愚蠢的问题,但我试图找出我的代码出了什么问题。

我有一个用户控件,里面有一个按钮。我有一个DependencyProperty声明,我将其绑定到用户控件内的按钮内容(是的,这很奇怪)。

现在我在我的主 xaml 页面中使用了这个用户控件的 2 个实例,并且依赖属性绑定到我的 ViewModel 中的一个属性。但是,绑定在运行时失败。

问题是 - 为什么它不起作用?

代码:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication2="clr-namespace:WpfApplication2" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <WpfApplication2:UserControl1 NewContent="{Binding CanEnableOne}" Grid.Row="0"/>
        <WpfApplication2:UserControl1 NewContent="{Binding CanEnableTwo}"  Grid.Row="1"/>
        <Button Grid.Row="2" Content="Enable Hello1" Click="OnClickedOne"/>
        <Button Grid.Row="3" Content="Enable Hello2" Click="OnClickedTwo"/>        
    </Grid>
</Window>


public partial class MainWindow : Window
{
    ViewModel vm = new ViewModel();
    Random r = new Random();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = vm;
    }

    private void OnClickedOne(object sender, RoutedEventArgs e)
    {
        vm.CanEnableOne = r.Next(1,100).ToString();
    }

    private void OnClickedTwo(object sender, RoutedEventArgs e)
    {
        vm.CanEnableTwo = r.Next(1, 100).ToString();
    }
}

public class ViewModel : INotifyPropertyChanged
{
    private string _canEnableOne;
    public string CanEnableOne
    {
        get { return _canEnableOne; }

        set
        {
            if (_canEnableOne == value)
                return;
            _canEnableOne = value;
            InvokePropertyChanged("CanEnableOne");
        }
    }


    private string _canEnableTwo;
    public string CanEnableTwo
    {
        get { return _canEnableTwo; }

        set
        {
            if (_canEnableTwo == value)
                return;
            _canEnableTwo = value;
            InvokePropertyChanged("CanEnableTwo");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void InvokePropertyChanged(string e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(e));
    }
}


<UserControl x:Class="WpfApplication2.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Button Content="{Binding NewContent}"></Button>    
    </Grid>
</UserControl>


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

    public static readonly DependencyProperty NewContentProperty =
        DependencyProperty.RegisterAttached("NewContent",
                                                   typeof (string),
                                                   typeof (UserControl1), new PropertyMetadata(new PropertyChangedCallback(PropertyChanged)));

    private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Hello");
    }

    public string NewContent
    {
        get { return (string)GetValue(NewContentProperty); }
        set { SetValue(NewContentProperty, value); }
    }

}

Error:System.Windows.Data Error: 39 : BindingExpression path error: 'NewContent' property not found on 'object' ''ViewModel' (HashCode=23172649)'. BindingExpression:Path=NewContent; DataItem='ViewModel' (HashCode=23172649); target element is 'Button' (Name=''); target property is 'Content' (type 'Object')
System.Windows.Data Error: 39 : BindingExpression path error: 'NewContent' property not found on 'object' ''ViewModel' (HashCode=23172649)'. BindingExpression:Path=NewContent; DataItem='ViewModel' (HashCode=23172649); target element is 'Button' (Name=''); target property is 'Content' (type 'Object')
4

1 回答 1

2

请记住,{Binding}将默认明智总是以DataContext作为属性的来源。这就是您的代码不起作用的原因,NewContent它不是您的 ViewModel 中的属性。

您想将绑定源更改为 UserControl,这是一种方法:

<UserControl x:Class="WpfApplication2.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" 
             d:DesignWidth="300"
             x:Name="myControl">
    <Grid>
        <Button Content="{Binding ElementName=myControl, Path=NewContent></Button>    
    </Grid>
</UserControl>
于 2013-07-05T15:38:15.113 回答