[英]WPF binding to a Usercontrol and getting an error
我從WPF開始,抱歉,如果我不能解釋好,我有幾個小時試圖解決如何將集合綁定到自定義網格名為PagingDataGrid。
PagingDataGrid在CustomerSearchControl中將GridItems綁定到ItemsSource,當我執行SearchCommand時,GridItems會更新,但沒有其他更改。
我收到以下錯誤:
System.Windows.Data Error: 40 : BindingExpression path error: 'GridItems' property not found on 'object' ''PagingDataGridViewModel' (HashCode=54151655)'. BindingExpression:Path=GridItems; DataItem='PagingDataGridViewModel' (HashCode=54151655); target element is 'PagingDataGrid' (Name='Me'); target property is 'ItemsSource' (type 'IEnumerable')
CustomerSearchControl:
<UserControl x:Class="Namespace.CustomerSearchControl"
... >
<Control.DataContext>
<Binding Path="CustomerSearchViewModel" ... />
</Control.DataContext>
<DockPanel LastChildFill="True">
<GroupBox Header="Registros">
<controls:PagingDataGrid ItemsSource="{Binding GridItems}" Height="300" />
</GroupBox>
</DockPanel>
</UserControl>
public class CustomerSearchViewModel : ViewModelBase
{
public ObservableCollection<GridItem> GridItems{ get; set; }
public ICommand SearchCommand { get; set; }
public CustomerSearchViewModel()
{
GridItems = new ObservableCollection<GridItem>();
SearchCommand = new RelayCommand(SearchEntities, () => true);
}
}
PagingDataGrid:
<UserControl x:Class="Namespace.PagingDataGrid" x:Name="Me"
... >
<UserControl.DataContext>
<Binding Path="PagingDataGridViewModel" ... />
</UserControl.DataContext>
<Grid>
...
<xcdg:DataGridControl
ItemsSource="{Binding ElementName=Me, Path=ItemsSource}" Grid.Row="0"/>
</Grid>
</UserControl>
public partial class PagingDataGrid : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty
= DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(PagingDataGrid),
new PropertyMetadata(default(IEnumerable)));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
}
您需要在XAML中聲明 CustomerSearchViewModel
實例並綁定到DataContext。
這是怎么做的:
<UserControl.DataContext> <local:CustomerSearchViewModel/> </UserControl.DataContext>
確保在root用戶身份聲明名稱空間 local
,即UserControl:
xmlns:local="clr-namespace:WpfApplication" <-- Replace WpfApplication with actual namespace of your ViewModel.
從ServiceLocator獲取實例后不需要。
要綁定到GridItems
您需要使用RelativeSource
顯式綁定到CustomerSearchControl
DataContext。 這是必需的,因為您已將PagingDataGrid
上的DataContext顯式設置為PagingDataGridViewModel
。 因此,它將在PagingDataGridViewModel而不是CustomerSearchViewModel中搜索GridItems
屬性。
<controls:PagingDataGrid ItemsSource="{Binding DataContext.GridItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
或者您可以將x:Name
給CustomerSearchControl
並使用ElementName
綁定。
永遠不要將usercontrol的datacontext設置為self。 所以簡單地刪除
<UserControl.DataContext>
<Binding Path="PagingDataGridViewModel" ... />
</UserControl.DataContext>
編輯:如果你不刪除它,那么你的
<UserControl x:Class="Namespace.PagingDataGrid" x:Name="Me">
<UserControl.DataContext>
<Binding Path="PagingDataGridViewModel" ... />
</UserControl.DataContext>
<Grid>
<xcdg:DataGridControl ItemsSource="{Binding ElementName=Me, Path=ItemsSource}" Grid.Row="0"/>
</Grid>
</UserControl>
你的usercontrol的datacontext是PagingDataGridViewModel,如果PagingDataGridViewModel沒有屬性ItemsSource,你會收到一個錯誤。 你永遠不會從你想要的CustomerSearchViewModel獲得GridItems。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.