[英]BindingExpression path error in wpf mvvm
I have problem in my binding and I don't know why. 我的装订有问题,我不知道为什么。 I am using Devexpress MVVM
. 我正在使用Devexpress MVVM
。 I have a user control which I used to populate textbox based on my ObservableCollection
which is working well. 我有一个用户控件,该控件用于根据运行良好的ObservableCollection
填充文本框。
I have this xaml code in my user control. 我的用户控件中有此xaml代码。
<ItemsControl IsTabStop="False" ItemsSource="{Binding ListControls}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding RGN_INdex}" />
<TextBox Text="{Binding RGN}" Grid.Column="1" >
<TextBox.InputBindings>
<KeyBinding Key="F1" Command="{Binding InsertControlCommand}" CommandParameter="{Binding Path=RGN_INdex }" />
</TextBox.InputBindings>
</TextBox>
<Label Grid.Column="2" Content="RSN:" />
<TextBox Text="{Binding RSN}" Grid.Column="3" />
<Label Grid.Column="4" Content="SGN:" />
<TextBox Text="{Binding SGN}" Grid.Column="5" />
<Label Grid.Column="6" Content="SN:" />
<TextBox Text="{Binding SN}" Grid.Column="7" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The above code is working until I add the InsertControlCommand
. 在添加InsertControlCommand
之前,以上代码一直有效。
<TextBox.InputBindings>
<KeyBinding Key="F1" Command="{Binding InsertControlCommand}" CommandParameter="{Binding Path=RGN_INdex }" />
</TextBox.InputBindings>
It gives me: 它给了我:
System.Windows.Data Error: 40 : BindingExpression path error: 'InsertControlCommand' property not found on 'object' ''RelativesM' (HashCode=41849684)'. BindingExpression:Path=InsertControlCommand; DataItem='RelativesM' (HashCode=41849684); target element is 'KeyBinding' (HashCode=2666760); target property is 'Command' (type 'ICommand')
Why does it doesn't find the InsertControlCommand
when that command and the ListControls
ObservableCollection are in the same Class(ViewModel)? 当该命令和ListControls
ObservableCollection位于同一Class(ViewModel)中时,为什么找不到InsertControlCommand
?
This is my ViewModelBase
where the InsertControlCommand
and ListControls
are located. 这是我的ViewModelBase
, InsertControlCommand
和ListControls
所在的位置。
public abstract class ViewModelBase : INotifyPropertyChanged {
public DelegateCommand<object> InsertControlCommand { get; private set; }
public ObservableCollection<Model.RelativesM> ListControls { get; set; }
public ViewModelBase() {
InsertControlCommand = new DelegateCommand<object>(InsertControlEvent);
ListControls = new ObservableCollection<Model.RelativesM>();
}
}
Then I have this MainViewModel: 然后我有这个MainViewModel:
public class MainViewModel : ViewModelBase {
}
Then on my MainWindow.xaml I set the DataContext with this code: 然后在我的MainWindow.xaml中,使用以下代码设置DataContext:
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
Upon my understanding my code should work. 据我了解,我的代码应该可以工作。 Because the ListControls
and InsertControlCommand
use the same ViewModel, but the command doesn't. 因为ListControls
和InsertControlCommand
使用相同的ViewModel,但该命令没有。 What is the reason? 是什么原因? What did I miss? 我错过了什么?
If you bind it like this, it wont look for the InsertControlCommand in your DataContext (MainViewModel) but in your RelativesM Class. 如果这样绑定它,它将不会在DataContext(MainViewModel)中而是在RelativesM类中寻找InsertControlCommand。 This is clearly displayed in your Error Message: 您的错误消息中清楚地显示了这一点:
'InsertControlCommand' property not found on 'object' ''RelativesM' 在“对象”“ RelativesM”上找不到“ InsertControlCommand”属性
Change your Binding of the Command to; 将您对命令的绑定更改为;
Command="{Binding DataContext.InsertControlCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"
And it should work as you expected. 它应该可以按预期工作。
Why does it doesn't find the InsertControlCommand when that command and the ListControls ObservableCollection are in the same Class(ViewModel)? 当该命令和ListControls ObservableCollection位于同一Class(ViewModel)中时,为什么找不到InsertControlCommand?
Because you have one TextBox
per item in the ObservableCollection<Model.RelativesM>
and the DataContext
of each TextBox
is the current RelativesM
model object in the ObservableCollection<Model.RelativesM>
and not the view model itself. 因为你有一个TextBox
每个项目在ObservableCollection<Model.RelativesM>
和DataContext
每个的TextBox
是当前RelativesM
在模型对象ObservableCollection<Model.RelativesM>
而不是视图模型本身。
To be able to bind to a property of the view model, you could use a {RelativeSource}
binding as suggested by @LittleBit: 为了能够绑定到视图模型的属性,可以使用@LittleBit建议的{RelativeSource}
绑定:
<TextBox Text="{Binding RGN}" Grid.Column="1" >
<TextBox.InputBindings>
<KeyBinding Key="F1" Command="{Binding DataContext.InsertControlCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding Path=RGN_INdex }" />
</TextBox.InputBindings>
</TextBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.