当DataGrid绑定到具有Sub属性的Observable Collection of Properties时,WPF Databinding不在两个方向上工作

时间:2014-01-22 02:22:44

标签: wpf data-binding wpfdatagrid observablecollection

原谅罗嗦的标题,我在简洁的描述中遇到了麻烦。如果我能想出一个,我可能谷歌正确答案!

我将DataGrid绑定到一个ObservableCollection属性,这些属性本身具有属性。我的网格填充得很好,但是当我编辑网格时,更改不会回到我的模型。 我有一个ObservableCollection

通常,你只有MarriedCoupleRow的一些属性,但实际上我有一些稍微复杂的东西。每个MarriedCoupleRow都有一些propties(男性,女性),这反过来暴露属性(身高,体重,信息)。这是可以编辑的信息。同样,我可以很好地填充网格,但是当你编辑单元格和标签关闭(或离开)时,信息的setter属性不会被命中。

我很感激任何指针或参考,包括如何更好地说出我的标题!

这是简单的代码:

 public class XMLDemoViewModel : ViewModelBase
{

    public XMLDemoViewModel()
    {

        _rows = new ObservableCollection<MarriedCoupleRow>();
        // create some data....
        for (uint i = 0; i < 2;i++)
        {
            MarriedCoupleRow row = new MarriedCoupleRow();
            row.Male = new HumanData();
            row.Male.Height = (70 + i*5).ToString();
            row.Male.Weight = 150+(i*30+1);
            row.Male.Information = row.Male.Height + " " + row.Male.Weight;

            row.Female = new HumanData();
            row.Female.Height = (60 +i*3).ToString();
            row.Female.Weight = 120+(i*10+5);
            row.Female.Information = row.Female.Height + " " + row.Female.Weight;
            _rows.Add(row);
        }
    }


    #region Fields

    private ObservableCollection<MarriedCoupleRow> _rows = null; 


    #endregion Fields

    #region Properties

    public ObservableCollection<MarriedCoupleRow> Rows
    {
        get
        {
            return _rows;

        }

    }
    #endregion Properties
    #region Commands


    #endregion Commands

    #region Private Methods


    #endregion Private Methods
}

public class MarriedCoupleRow : ViewModelBase
{
    private HumanData _Male = null;

    public HumanData Male
    {
        get { return _Male; }
        set
        {
            if (value != _Male)
            {
                _Male = value;
                OnPropertyChanged("Male");
            }
        }
    }
    public HumanData Female { get; set; }

}

public class HumanData : INotifyPropertyChanged
{
    public string Height { get; set; }
    public uint Weight { get; set; }

    private string _friendlyName;
    public string Information
    {
        get
        {
            return _friendlyName;

        }
        set
        {
            if (_friendlyName != value)
            {
                _friendlyName = value;
                OnPropertyChanged("Information");
            }
        }
    }


}

这是XAML:

<Window x:Class="XMLDemo.Views.XMLDemoView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="XMLDemoView"  Height="600" Width="1152">

    <DockPanel>
        <StackPanel Orientation="Horizontal" Height="120" DockPanel.Dock="Bottom">
            <StackPanel Orientation="Horizontal">
                <GroupBox Width="249" BorderThickness="2" Height="90">
                    <GroupBox.Header>
                        <TextBlock FontSize="12" FontWeight="Bold">Control</TextBlock>
                    </GroupBox.Header>
                    <Grid Height="64" Width="223">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.5*" />
                            <ColumnDefinition Width="0.5*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.5*"/>
                            <RowDefinition Height="0.5*"/>
                        </Grid.RowDefinitions>

                    </Grid>

                </GroupBox>
            </StackPanel>
        </StackPanel>
    <DataGrid ItemsSource="{Binding Path=Rows,  UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">

        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate >
                    <DataTemplate>
                        <Grid ShowGridLines="True">
                            <Grid.RowDefinitions>
                                <RowDefinition />

                            </Grid.RowDefinitions>
                            <TextBlock Text="Male"  />

                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBox Text="{Binding Male.Height}" IsEnabled="False"  Grid.Row="0"></TextBox>
                            <TextBox Text="{Binding Male.Weight}" IsEnabled="False" Grid.Row="1"></TextBox>
                            <TextBox Text="{Binding Male.Information, Mode=TwoWay}"   Grid.Row="2"></TextBox>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate >
                    <DataTemplate>
                        <Grid ShowGridLines="True">
                            <Grid.RowDefinitions>
                                <RowDefinition />

                            </Grid.RowDefinitions>
                            <TextBlock Text="Female"  />

                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBox Text="{Binding Female.Height}" IsEnabled="False"  Grid.Row="0"></TextBox>
                            <TextBox Text="{Binding Female.Weight}" IsEnabled="False" Grid.Row="1"></TextBox>
                            <TextBox Text="{Binding Female.Information}" Grid.Row="2"></TextBox>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>
    </DockPanel>

2 个答案:

答案 0 :(得分:1)

您的TextBox.Text绑定需要在propertychanged上设置为updatesource。

<TextBox Text="{Binding Male.Information, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"   Grid.Row="2"></TextBox>

默认情况下,LostFocus肯定会出现一些愚蠢的事情。我使用你的代码对此进行了测试。

答案 1 :(得分:0)

我喜欢你的帖子。正如我从编码中看到的那样,您正在使用与DataGrid的Complex属性绑定。此外,复杂的属性更改也不会反映在DataGrid中。

但是,您可以在样本级别中实现此要求。我会尝试制作样本并让你知道。

此致 Riyaj Ahamed I