• 2018-8-10-wpf-绑定-DataGridTextColumn-


    title author date CreateTime categories
    wpf 绑定 DataGridTextColumn
    lindexi
    2018-08-10 19:16:53 +0800
    2018-2-13 17:23:3 +0800
    WPF

    本文告诉大家如何在 DataGridTextColumn 使用绑定,因为很容易绑定就找不到数据。

    使用 DataGrid 有一个坑,就是 Columns 的数据绑定拿不到数据。

    例如下面的代码

       <Grid>
            <DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataList}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
                    <DataGridTextColumn Header="Property1" Binding="{Binding Property1}"/>
                    <DataGridTextColumn Header="Property2" Binding="{Binding Property2}"/>
              
                    <DataGridTemplateColumn Header="Total">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock>
                                    <TextBlock.Text>
                                        <MultiBinding>
                                            <Binding Path="Property1"/>
                                            <Binding Path="Property2"/>
                                            <MultiBinding.Converter>
                                                <local:MyValueConverter/>
                                            </MultiBinding.Converter>
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

    可以看到绑定了DataGridTextColumn Header="Property1" Binding="{Binding Property1}" 可以拿到数值,但是在MultiBinding没有拿到数值,因为他在DataTemplateDataGridTemplateColumn没有数据。

    解决方法很简单,使用RelativeSource找到数据。只需要修改<Binding Path="DataContext.Property2" RelativeSource="{RelativeSource AncestorType=DataGridCell}"/> 就可以。下面就是修改后的代码

      <Grid>
            <DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataList}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
                    <DataGridTextColumn Header="Property1" Binding="{Binding Property1}"/>
                    <DataGridTextColumn Header="Property2" Binding="{Binding Property2}"/>
                    <DataGridTextColumn>
                        <DataGridTextColumn.Binding >
                            <MultiBinding >
                                <Binding Path="DataContext.Property1" RelativeSource="{RelativeSource AncestorType=DataGridCell}"/>
                                <Binding Path="DataContext.Property2" RelativeSource="{RelativeSource AncestorType=DataGridCell}"/>
                                <MultiBinding.Converter>
                                    <local:MyValueConverter/>
                                </MultiBinding.Converter>
                            </MultiBinding>
                        </DataGridTextColumn.Binding>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

  • 相关阅读:
    MFC绘图基础
    MFC绘图基础
    MFC坐标问题
    利用Graphziv帮助理解复杂的类层次关系
    priority_quenue
    1060. Are They Equal (25)
    1057. Stack (30)
    1056. Mice and Rice (25)
    1053. Path of Equal Weight (30)
    1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085544.html
Copyright © 2020-2023  润新知