选择不会隐藏DataGridCell [英] Selection does hide the DataGridCell

查看:85
本文介绍了选择不会隐藏DataGridCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格:

 < DataGrid x:Name ="grid1"Horizo​​ntalAlignment =拉伸";VerticalAlignment =拉伸"ItemsSource ="{Binding}"IsReadOnly =真".已加载="grid1_Loaded";AutoGeneratingColumn ="grid1_AutoGeneratingColumn";SelectionUnit =单元".MouseMove ="Grid1_MouseMove";LoadingRow ="grid1_LoadingRow";MouseLeave ="grid1_MouseLeave".< DataGrid.Resources>< Style TargetType ="{x:Type DataGridCell}"< Setter Property =模板">< Setter.Value>< ControlTemplate TargetType ="{x:Type DataGridCell}"< TextBlock Text =< {Binding RelativeSource = {RelativeSource TemplatedParent},Path = Content.Text}"TextTrimming ="CharacterEllipsis".< TextBlock.ToolTip>< ToolTip Visibility ="{Binding RelativeSource = {RelativeSource Self},Path = PlacementTarget,Converter = {StaticResource trimmedVisibilityConverter}}">< ToolTip.Content>< TextBlock Text ="{Binding RelativeSource = {RelativeSource TemplatedParent},Path = Content.Text}"//</ToolTip.Content></ToolTip></TextBlock.ToolTip></TextBlock></ControlTemplate></Setter.Value></Setter></样式></DataGrid.Resources>< DataGrid.RowStyle>< Style TargetType ="{x:Type DataGridRow}"< Setter Property =前景";值=红色"/> 0.< Style.Triggers>< DataTrigger Binding =" {BindingРасторжение}"值="{x:Null}"< Setter Property =前景";值="Black"</Setter>.</DataTrigger></Style.Triggers></样式></DataGrid.RowStyle></DataGrid> 

还有一个转换器:

 公共类TrimmedTextBlockVisibilityConverter:IValueConverter{公共对象Convert(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化){if(value == null)返回Visibility.Collapsed;FrameworkElement textBlock =(FrameworkElement)value;textBlock.Measure(新System.Windows.Size(Double.PositiveInfinity,Double.PositiveInfinity));如果((((FrameworkElement)value).ActualWidth<(((FrameworkElement)value).DesiredSize.Width)返回Visibility.Visible;别的返回Visibility.Collapsed;//Visibility.Collapsed;}公共对象ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化){抛出新的NotImplementedException();}} 

工具提示,在修剪单元格内容时打开,位于 DataGrid.Resources 部分,我从

当我选择一个或多个单元格时,其中的所有值都会消失,并且单元格不会突出显示... DataGrid 的项目来源是 DataTable ,如果有的话事情.我该如何解决这个问题?

解决方案

这很有趣!只需设置 DataGrid.CellStyle 即可重现该问题.

我认为问题在于, ControlTemplate 中的 Background 颜色是白色,因此看不到选择.

TextBlock 添加 Background ="{TemplateBinding Background}" .

I have a data grid:

<DataGrid x:Name="grid1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" IsReadOnly="True"
              Loaded="grid1_Loaded" AutoGeneratingColumn="grid1_AutoGeneratingColumn" SelectionUnit="Cell" MouseMove="Grid1_MouseMove" LoadingRow="grid1_LoadingRow" MouseLeave="grid1_MouseLeave">

                    <DataGrid.Resources>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextTrimming="CharacterEllipsis">
                                            <TextBlock.ToolTip>
                                                <ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
                                                    <ToolTip.Content>
                                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"/>
                                                    </ToolTip.Content>
                                                </ToolTip>
                                            </TextBlock.ToolTip>
                                        </TextBlock>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGrid.Resources>

                    <DataGrid.RowStyle>
                        <Style TargetType="{x:Type DataGridRow}">
                            <Setter Property="Foreground" Value="Red"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Расторжение}" Value="{x:Null}">
                                    <Setter Property="Foreground" Value="Black"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>
                </DataGrid>

And a converter:

public class TrimmedTextBlockVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return Visibility.Collapsed;

        FrameworkElement textBlock = (FrameworkElement)value;

        textBlock.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));

        if (((FrameworkElement)value).ActualWidth < ((FrameworkElement)value).DesiredSize.Width)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;//Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

ToolTip, that opens when cell content is trimmed, is in DataGrid.Resources part, I get it from here. It works, but now, when I click any cell it looks like this:

When I am selecting a cell or cells, all values in it disappear and cell is not highlighted... Items source of my DataGrid is DataTable, if it does matter. How can I fix this issue?

解决方案

It's interesting! Just setting the DataGrid.CellStyle does reproduce the issue.

I think, that the problem is, that the Background color in ControlTemplate is white, so you don't see the selection.

Add Background="{TemplateBinding Background}" to the TextBlock.

这篇关于选择不会隐藏DataGridCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆