gpt4 book ai didi

c# - 绑定(bind)到 ViewModel 属性时,WPF 用户控件依赖属性不起作用

转载 作者:行者123 更新时间:2023-12-03 10:46:10 25 4
gpt4 key购买 nike

我有两个用户控件:一个 LocationTreeView 和一个 LocationPicker。 LocationTreeView 将 Locations 组织成一个树形结构。由于涉及的位置数量,一次只加载树的一部分(随着项目的展开,一次加载一个级别)。

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

当我将 LocationPicker 的“SelectedLocation”属性绑定(bind)到我的 Viewmodel 时,它工作正常。当我将 LocationTreeView 绑定(bind)到 View 模型时,绑定(bind)似乎根本没有任何效果。当我将我的 LocationTreeView 绑定(bind)到“虚拟”LocationPicker(绑定(bind)到我的 View 模型)时,它可以工作。
如何让我的 LocationTreeView 绑定(bind)到我的 View 模型?

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);
}
}
}
}
...
}

绑定(bind)到另一个控件时,此控件上的绑定(bind)可以正常工作,但绑定(bind)到我的 View 模型时则不行。我在 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>

最佳答案

如果你想从你的 View 模型数据绑定(bind)到 LocationTreeView ,那么您应该使用 View 模型中的属性来进行数据绑定(bind)。如果您的 View 模型有一个名为 SelectedLocationInViewModel 的属性在其中,您应该使用它来将数据绑定(bind)到:

<UC:LocationTreeView SelectedLocation="{Binding SelectedLocationInViewModel}" />

我想我知道你现在的问题是什么了......你想在 UserControl 中定义一些属性和数据绑定(bind)到它们,而且数据也绑定(bind)到设置为 DataContext 的 View 模型的属性。 .您需要使用 RelativeSource Binding要做到这一点...只需查看 Binding Path s 在这些例子中:

数据绑定(bind)到 UserControl 中声明的属性来自 UserControl :
<ItemsControl ItemsSource="{Binding PropertyName, RelativeSource={RelativeSource 
AncestorType={x:Type YourPrefix:YourUserControl}}}" />

数据绑定(bind)到在任何设置为 DataContext 的对象中声明的属性:
<ItemsControl ItemsSource="{Binding PropertyName}" />

关于c# - 绑定(bind)到 ViewModel 属性时,WPF 用户控件依赖属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25371904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com