首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当绑定到ViewModel属性时,WPF用户控件依赖项属性无法工作

当绑定到ViewModel属性时,WPF用户控件依赖项属性无法工作
EN

Stack Overflow用户
提问于 2014-08-18 20:57:34
回答 1查看 869关注 0票数 1

我有两个用户控件:一个LocationTreeView和一个LocationPicker。LocationTreeView将位置组织到树结构中。由于所涉及的位置数,一次只加载树的一部分(一次加载一个级别,然后展开项)。

LocationPicker只不过是一个带有按钮的文本块,该按钮打开了一个带有LocationTreeView的模态窗口。

当我将LocationPicker的"SelectedLocation“属性绑定到我的视图模型时,它工作得很好。当我将我的LocationTreeView绑定到视图模型时,绑定似乎没有任何效果。当我将我的LocationTreeView绑定到一个“虚拟”LocationPicker (它绑定到我的视图模型)时,它可以工作。如何使我的LocationTreeView绑定到我的视图模型?

代码语言:javascript
代码运行次数:0
运行
复制
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)时,它似乎不会被触发。

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
<UserControl.DataContext>
    <VM:LocationTreeViewViewModel />
</UserControl.DataContext>

LocationPicker没有ViewModel。在我使用控件的窗口上,xaml看起来如下所示

代码语言:javascript
代码运行次数:0
运行
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2014-08-19 08:06:56

如果希望将数据绑定从视图模型绑定到LocationTreeView,则应使用要绑定到的视图模型中的属性。如果视图模型中有一个名为SelectedLocationInViewModel的属性,那么您应该使用它来绑定到:

代码语言:javascript
代码运行次数:0
运行
复制
<UC:LocationTreeView SelectedLocation="{Binding SelectedLocationInViewModel}" />

我想我知道你现在有什么问题了.您希望在UserControl中定义一些属性并将数据绑定到它们,但也希望将数据绑定到设置为DataContext的视图模型中的属性。你需要用RelativeSource Binding来做.只需查看以下示例中的Binding Path

将数据绑定到UserControl中从UserControl中声明的属性

代码语言:javascript
代码运行次数:0
运行
复制
<ItemsControl ItemsSource="{Binding PropertyName, RelativeSource={RelativeSource 
    AncestorType={x:Type YourPrefix:YourUserControl}}}" />

将数据绑定到在任何对象集中声明为DataContext的属性

代码语言:javascript
代码运行次数:0
运行
复制
<ItemsControl ItemsSource="{Binding PropertyName}" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25371904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档