我有两个用户控件:一个LocationTreeView和一个LocationPicker。LocationTreeView将位置组织到树结构中。由于所涉及的位置数,一次只加载树的一部分(一次加载一个级别,然后展开项)。
LocationPicker只不过是一个带有按钮的文本块,该按钮打开了一个带有LocationTreeView的模态窗口。
当我将LocationPicker的"SelectedLocation“属性绑定到我的视图模型时,它工作得很好。当我将我的LocationTreeView绑定到视图模型时,绑定似乎没有任何效果。当我将我的LocationTreeView绑定到一个“虚拟”LocationPicker (它绑定到我的视图模型)时,它可以工作。如何使我的LocationTreeView绑定到我的视图模型?
public partial class LocationTreeView: UserControl
{
public EventHandler LocationChanged;
...
public static readonly DependencyProperty SelectedLocationProperty =
DependencyProperty.Register("SelectedLocation",typeof(Location), typeof(LocationTreeView),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, SelectedLocationChanged));
...
public static void SelectedLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
LocationTreeView sender = (d as LocationTreeView);
Location loc = e.NewValue as Location;
//Navigate the treeview to the selected location
sender.LoadLTreeViewPathToLocation(loc);
}
public Location SelectedLocation
{
get { return (Location)GetValue(SelectedLocationProperty); }
set
{
if (SelectedLocation != value)
{
SetValue(SelectedLocationProperty, value);
if (LocationChanged != null)
{
LocationChanged(this, EventArgs.Empty);
}
}
}
}
...
}
绑定此控件时,绑定到另一个控件时工作良好,但绑定到我的视图模型时不起作用。我在SelectedLocationChanged回调中设置了一个断点,当我设置viewmodel属性(它确实实现了INotifyPropertyChanged)时,它似乎不会被触发。
public partial class LocationPicker: UserControl
{
public static readonly DependencyProperty SelectedLocationProperty =
DependencyProperty.Register("SelectedLocation",typeof(Location), typeof(LocationPicker),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
...
public Location SelectedLocation
{
get { return (Location)GetValue(SelectedLocationProperty); }
set { SetValue(SelectedLocationProperty, value); }
}
...
private void Button_Click(object sender, RoutedEventArgs e)
{
// create a window with a locationtreeview on it. Set the treeview's
// selectedlocation property, open the window, wait for the window to close,
// set this.SelectedLoctation to the treeview's selected location.
}
}
我很抱歉遗漏了这么多代码。我的工作环境使我无法复制/粘贴。
我遗漏了ViewModel的代码。我深信这不是问题所在。
更新: LocationTreeView有一个在xaml中设置的ViewModel
<UserControl.DataContext>
<VM:LocationTreeViewViewModel />
</UserControl.DataContext>
LocationPicker没有ViewModel。在我使用控件的窗口上,xaml看起来如下所示
<Widow.DataContext>
<VM:TestWindowViewModel />
</Window.DataContext>
<Grid>
...
<UC:LocationPicker x:Name="picker" SelectedLocation="{Binding Location}" />
<!-- this does not work -->
<UC:LocationTreeView SelectedLocaiton="{Binding Location}" />
<!-- but this works --->
<UC:LocationTreeView SelectedLocaiton="{Binding SelectedLocation, ElementName=picker}" />
...
</Grid>
发布于 2014-08-19 00:06:56
如果希望将数据绑定从视图模型绑定到LocationTreeView
,则应使用要绑定到的视图模型中的属性。如果视图模型中有一个名为SelectedLocationInViewModel
的属性,那么您应该使用它来绑定到:
<UC:LocationTreeView SelectedLocation="{Binding SelectedLocationInViewModel}" />
我想我知道你现在有什么问题了.您希望在UserControl
中定义一些属性并将数据绑定到它们,但也希望将数据绑定到设置为DataContext
的视图模型中的属性。你需要用RelativeSource Binding
来做.只需查看以下示例中的Binding Path
:
将数据绑定到UserControl
中从UserControl
中声明的属性
<ItemsControl ItemsSource="{Binding PropertyName, RelativeSource={RelativeSource
AncestorType={x:Type YourPrefix:YourUserControl}}}" />
将数据绑定到在任何对象集中声明为DataContext
的属性
<ItemsControl ItemsSource="{Binding PropertyName}" />
https://stackoverflow.com/questions/25371904
复制相似问题