DataGrid 数据绑定样式问题

发布于 2024-12-04 14:19:41 字数 2065 浏览 0 评论 0原文

我有一个 WPF DataGrid,我想对所有单元格应用文本换行,所以我定义了这种样式:

        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" FontSize="15" Text="{Binding}"  VerticalAlignment="Center" 
                                 HorizontalAlignment="Center" ></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.Columns>

            <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="95" />
            <DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="95"  />
            <DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="95" />

        </DataGrid.Columns>

并且我在后面的代码中设置了 DataGrid 的 ItemsSource,如下所示:

        myDataGrid.ItemsSource= new Customers[] 
        { 
                new ComputerStandard{Name="Michael Thomas",Address="16 Greenville Avenue",Category="A"},            
                new ComputerStandard{Name="Fiona Thompson",Address="19 Wright Street",Category="F"},            
                new ComputerStandard{Name="Jack Smith",Address="133 Kensington Road",Category="B"},
                new ComputerStandard{Name="Michael jackson",Address="11 Wine Street",Category="C"},
                new ComputerStandard{Name="Jerry Holmes",Address="10 Awson Street",Category="G"},
                new ComputerStandard{Name="David Philips",Address="Not Specified",Category="A"}
        };

但是在某些地方,我设置的绑定表达式失败了我的风格 Text="{Binding}" 最后得到:

在此处输入图像描述

显然绑定表达式Text="{Binding}" 失败了,我知道这一点,因为当我删除样式时,一切都工作正常。我该如何解决这个问题?

提前致谢。

I have a WPF DataGrid and I want to apply text wrapping to all the cells, so I've defined this style:

        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" FontSize="15" Text="{Binding}"  VerticalAlignment="Center" 
                                 HorizontalAlignment="Center" ></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.Columns>

            <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="95" />
            <DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="95"  />
            <DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="95" />

        </DataGrid.Columns>

and I set my DataGrid's ItemsSource in my code behind like this:

        myDataGrid.ItemsSource= new Customers[] 
        { 
                new ComputerStandard{Name="Michael Thomas",Address="16 Greenville Avenue",Category="A"},            
                new ComputerStandard{Name="Fiona Thompson",Address="19 Wright Street",Category="F"},            
                new ComputerStandard{Name="Jack Smith",Address="133 Kensington Road",Category="B"},
                new ComputerStandard{Name="Michael jackson",Address="11 Wine Street",Category="C"},
                new ComputerStandard{Name="Jerry Holmes",Address="10 Awson Street",Category="G"},
                new ComputerStandard{Name="David Philips",Address="Not Specified",Category="A"}
        };

But somewhere something fails with my binding expression that I set in my style Text="{Binding}" and I end up with:

enter image description here

Obviously the binding expression Text="{Binding}" is failing, I know this because when I remove the style everything works perfectly. How do I go about fixing this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻之岛 2024-12-11 14:19:41

设置DataGridColumn.Binding 属性不会为每个DataGridCell 设置DataContextDataContext 仍然等于整个 Row 的 DataContext

从绑定 ContentTemplate 切换到绑定 Template,并且然后您就可以访问 ContentPresenter

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="DataGridCell">
            <TextBlock TextWrapping="Wrap" FontSize="15" 
                       VerticalAlignment="Center" HorizontalAlignment="Center">

                <ContentPresenter Content="{TemplateBinding Content}" />

            </TextBlock>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Setting the DataGridColumn.Binding property does not set the DataContext for each DataGridCell. The DataContext is still equal to the entire Row's DataContext

Switch from binding the ContentTemplate, to binding the Template, and then you have access to the ContentPresenter

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="DataGridCell">
            <TextBlock TextWrapping="Wrap" FontSize="15" 
                       VerticalAlignment="Center" HorizontalAlignment="Center">

                <ContentPresenter Content="{TemplateBinding Content}" />

            </TextBlock>
        </ControlTemplate>
    </Setter.Value>
</Setter>
我也只是我 2024-12-11 14:19:41

这将有所帮助:

<Style x:Key="MyGrid" TargetType="{x:Type DataGridCell}">    
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
               <Border x:Name="MyBorder" >
                  <TextBlock Background="Transparent" TextWrapping="WrapWithOverflow"
                     TextTrimming="CharacterEllipsis" Height="auto" Width="auto"> 
                   <ContentPresenter 
                     Content="{TemplateBinding Property=ContentControl.Content}" 
                     ContentTemplate="{TemplateBinding Property=ContentControl.Content}"/>
                  </TextBlock>
               </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

摘自此处。

This will help :

<Style x:Key="MyGrid" TargetType="{x:Type DataGridCell}">    
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
               <Border x:Name="MyBorder" >
                  <TextBlock Background="Transparent" TextWrapping="WrapWithOverflow"
                     TextTrimming="CharacterEllipsis" Height="auto" Width="auto"> 
                   <ContentPresenter 
                     Content="{TemplateBinding Property=ContentControl.Content}" 
                     ContentTemplate="{TemplateBinding Property=ContentControl.Content}"/>
                  </TextBlock>
               </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Taken from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文