[英]WPF User Control Dependency Property not working when bound to a ViewModel property
我有兩個用戶控件:LocationTreeView和LocationPicker。 LocationTreeView將位置組織成一個樹形結構。 由於涉及的位置數量眾多,因此一次僅加載樹的一部分(隨着項目的擴展,每次加載一個級別)。
LocationPicker只是一個帶有按鈕的文本塊,該按鈕可打開帶有LocationTreeView的模式窗口。
當我將LocationPicker的“ SelectedLocation”屬性綁定到我的Viewmodel時,它可以正常工作。 當我將LocationTreeView綁定到視圖模型時,綁定似乎根本沒有任何作用。 當我將LocationTreeView綁定到“虛擬” LocationPicker(綁定到我的viewmodel)時,它可以工作。 如何獲取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>
如果要將數據從視圖模型綁定到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}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.