UserControl中的数据绑定问题

时间:2013-04-18 22:36:45

标签: c# wpf data-binding

我正在制作一个execise应用程序。我的应用程序采用MVVM。我正在尝试使用两个用户控件创建一个窗口,其中一个用户控件包含一个从viewmodel获取其数据的datagrid。 我希望当应用程序运行时,datagrid会自动填充我的默认值(私有文件)。但是存在绑定错误: System.Windows.Data错误:4:无法找到与引用'ElementName = windowView'绑定的源。 BindingExpression:路径= ActivePacket;的DataItem = NULL; target元素是'DataGrid'(Name ='dataGrid1'); target属性是'ItemsSource'(类型'IEnumerable')

谢谢!

这是我的代码:

========= MainWindow.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 ======================== / p>

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");
                }
            }
        }
        // .........
 }

2 个答案:

答案 0 :(得分:3)

您必须在DataContext中为MainWindow.xaml分配<local:PayloadView ... DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext}" />

PayloadView.xaml

<DataGrid ... ItemsSource="{Binding ActivePacket}" /> 中,只需绑定到视图模型属性:

{{1}}

答案 1 :(得分:0)

一些想法...... 如果除PayloadView之外的任何地方都不需要任何PayloadViewModel,您可以直接在PayloadView的DataContext中绑定PayloadViewModel,然后直接绑定到它。

<UserControl x:Class="Project.Abc.Try.PayloadView" ...>

    <UserControl.DataContext>
        <local:PayloadViewModel />
    </UserControl.DataContext>

    ...

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

    ...

</UserControl>

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

<UserControl x:Class="Project.Abc.Try.PayloadView" ...>

    ...

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

    ...

</UserControl>

这是Caliburn.Micro的一个很棒的教程:http://www.mindscapehq.com/blog/index.php/2012/01/12/caliburn-micro-part-1-getting-started/

希望这有帮助。