繁体   English   中英

wpf mvvm中的BindingExpression路径错误

[英]BindingExpression path error in wpf mvvm

我的装订有问题,我不知道为什么。 我正在使用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')

当该命令和ListControls ObservableCollection位于同一Class(ViewModel)中时,为什么找不到InsertControlCommand

这是我的ViewModelBaseInsertControlCommandListControls所在的位置。

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,但该命令没有。 是什么原因? 我错过了什么?

如果这样绑定它,它将不会在DataContext(MainViewModel)中而是在RelativesM类中寻找InsertControlCommand。 您的错误消息中清楚地显示了这一点:

在“对象”“ RelativesM”上找不到“ InsertControlCommand”属性

将您对命令的绑定更改为;

Command="{Binding DataContext.InsertControlCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"

它应该可以按预期工作。

当该命令和ListControls ObservableCollection位于同一Class(ViewModel)中时,为什么找不到InsertControlCommand?

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

为了能够绑定到视图模型的属性,可以使用@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM