UserControl中的数据绑定问题

本文关键字:问题 数据绑定 UserControl | 更新日期: 2023-09-27 18:09:51

我正在做一个运动app,我的app采用MVVM。我试图创建一个有两个用户控件的窗口,其中一个用户控件包括一个数据网格,它从视图模型中获取数据。我希望数据网格在应用程序运行时自动填充我的默认值(私有字段)。但是存在绑定错误:System.Windows.Data错误:4:无法找到与引用"ElementName=windowView"绑定的源。BindingExpression:路径= ActivePacket;DataItem =零;目标元素是'DataGrid' (Name='dataGrid1');目标属性为"ItemsSource"(类型为"IEnumerable")

谢谢!

下面是我的代码:

========= 主窗口。xaml ================

    <Window x:Class="Project.Abc.Try.MainWindow"
        x:Name="windowView"
        xmlns:local="clr-namespace:Project.Abc.Try"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="600">
    <Window.DataContext>
        <local:PayloadViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="78*" />
            <RowDefinition Height="233*" />
        </Grid.RowDefinitions>
        <local:CmdMenuView Grid.Row="0" Margin="6,6,3,6" />
        <local:PayloadView Grid.Row="1" Margin="6,6,3,6" />
    </Grid>
</Window>

============ PayloadView。xaml ===========

    <UserControl x:Class="Project.Abc.Try.PayloadView"
             x:Name="PLview"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="199" d:DesignWidth="588" >
    <Grid Height="200" Width="580" >
        <!--<DataGrid AutoGenerateColumns="False" Height="45" HorizontalAlignment="Left" Margin="36,20,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="500" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActivePacket}"> -->
        <DataGrid AutoGenerateColumns="False" Height="45" HorizontalAlignment="Left" Margin="36,20,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="500" ItemsSource="{Binding ElementName=windowView, Path=ActivePacket}">
                <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding PacketId, Mode=TwoWay}" Header="PacketID " Width="*" />
                <DataGridTextColumn Binding="{Binding PacketLength, Mode=TwoWay}" Header="PacketLength" Width="*" />
                <DataGridTextColumn Binding="{Binding Spare}" Header="Byte 6" Width="*" />
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Send" Command="{Binding Path=SendCommand}" Height="23" HorizontalAlignment="Left" Margin="310,122,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>

=================== PayloadViewModel.cs ========================

namespace Project.Abc.Try
{
    public class PayloadViewModel : ObservableObject
    {
        // ......
        private CmdPacket _activePacket;
        public CmdPacket ActivePacket
        {
            get { return _activePacket; }
            set
            {
                if (value != _activePacket)
                {
                    _activePacket = value;
                    OnPropertyChanged("ActivePacket");
                }
            }
        }
        // .........
 }

UserControl中的数据绑定问题

您必须将DataContext分配给MainWindow.xaml中的视图:

<local:PayloadView ... DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext}" />

PayloadView.xaml中,只需绑定到视图模型属性:

<DataGrid ... ItemsSource="{Binding ActivePacket}" />

一些想法…如果你不需要任何来自PayloadViewModel的东西,除了你的PayloadView,你可以直接在你的PayloadView的DataContext中绑定PayloadViewModel,然后直接绑定到它。

<UserControl x:Class="Project.Abc.Try.PayloadView" ...>
    <UserControl.DataContext>
        <local:PayloadViewModel />
    </UserControl.DataContext>
    ...
    <DataGrid ... ItemsSource="{Binding ActivePacket}" />
    ...
</UserControl>

另外,如果你想做MVVM,你可能想考虑使用Caliburn。如果你没有的话。它自动绑定每个视图的数据上下文到相应的ViewModel没有代码(即PayloadView将自动访问PayloadViewModel上的公共属性)。那么你所要写的就是:

<UserControl x:Class="Project.Abc.Try.PayloadView" ...>
    ...
    <DataGrid ... ItemsSource="{Binding ActivePacket}" />
    ...
</UserControl>

这是一个伟大的教程的calburn。微观:http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/