1

为什么我无法绑定 datagrid 属性选定项?我的 ViewModel 中有财产

    protected TheFile curentFile ;
    public TheFile CurentFile
    {
        get { return curentFile; }
        set { curentFile = value;
            OnPropertyChanged("CurentFile"); }
    }

这是我的数据网格:

    <DataGrid IsReadOnly="True" AutoGenerateColumns="False" Grid.ColumnSpan="1" Grid.Row="1" Grid.Column="0" Grid.RowSpan="1"   Name="dataGrid1" ItemsSource="{Binding SameFileGroups}" SelectedItem="{Binding CurentGroup}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Всього" Width="1*" Binding="{Binding TotalSizeInDirectory}"/>
            <DataGridTextColumn Header="1 файл" Width="1*" Binding="{Binding OneFileSize}"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Path=Files}" AutoGenerateColumns="False" SelectedItem="{Binding Path=CurentFile, UpdateSourceTrigger=PropertyChanged}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Шлях" Binding="{Binding Path=Destination}"/>
                        <DataGridTextColumn Header="Розмір" Binding="{Binding Path=SizeBytes}"/>
                        <DataGridTextColumn Header="md5" Binding="{Binding Path=Md5Hash}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

但是这个绑定不起作用

   <DataGrid ItemsSource="{Binding Path=Files}" AutoGenerateColumns="False" SelectedItem="{Binding Path=CurentFile, UpdateSourceTrigger=PropertyChanged}">
4

1 回答 1

1

你这里有问题DataContext

RowDetailsTemplate您尝试绑定到主视图模型中的属性时,但DataContextofRowDetailsTemplate是行项。

如果要绑定到主视图模型中的属性,则应使用RelativeSource绑定。

尝试这个:

<DataGrid ItemsSource="{Binding Path=Files}" 
          SelectedItem="{Binding Path=DataContext.CurentFile, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
          AutoGenerateColumns="False" >
    ...
于 2013-07-16T17:55:27.443 回答