gpt4 book ai didi

c# - wpf mvvm中的BindingExpression路径错误

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

我的绑定(bind)有问题,我不知道为什么。我正在使用 Devexpress MVVM .我有一个用户控件,用于根据我的 ObservableCollection 填充文本框这运作良好。

我的用户控件中有这个 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>

上面的代码一直有效,直到我添加 InsertControlCommand .
<TextBox.InputBindings>
<KeyBinding Key="F1" Command="{Binding InsertControlCommand}" CommandParameter="{Binding Path=RGN_INdex }" />
</TextBox.InputBindings>

它给了我:
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')

为什么找不到 InsertControlCommand当那个命令和 ListControls ObservableCollection 在同一个类(ViewModel)中?

这是我的 ViewModelBase InsertControlCommandListControls位于。
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>();
}
}

然后我有这个 MainViewModel:
public class MainViewModel : ViewModelBase {
}

然后在我的 MainWindow.xaml 上,我使用以下代码设置 DataContext:
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>

据我了解,我的代码应该可以工作。因为 ListControlsInsertControlCommand使用相同的 ViewModel,但命令没有。是什么原因?我错过了什么?

最佳答案

Why does it doesn't find the InsertControlCommand when that command and the ListControls ObservableCollection are in the same Class(ViewModel)?



因为你有一个 TextBox ObservableCollection<Model.RelativesM> 中的每个项目和 DataContext每个 TextBox是当前 RelativesM ObservableCollection<Model.RelativesM> 中的模型对象而不是 View 模型本身。

为了能够绑定(bind)到 View 模型的属性,您可以使用 {RelativeSource} @LittleBit 建议的绑定(bind):
<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>

关于c# - wpf mvvm中的BindingExpression路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991696/

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