这可能是一个非常愚蠢的问题,但我正试图找出我的代码出了什么问题。
我有一个用户控件,里面有一个按钮。我声明了DependencyProperty
,我绑定到用户控件内部按钮的内容(是的,这很奇怪)。
现在我在我的主xaml页面中使用了这个用户控件的2个实例,并且Dependency属性绑定到了我的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')
答案 0 :(得分: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>