简体   繁体   English

WPF适合数据网格单元格Content.Text与DataGridCell宽度

[英]WPF Fit Data grid cell Content.Text with DataGridCell width

Hi I have a question and probably it is fairly simple, I have a data grid with styled cells and data triggers that xaml looks like: 嗨,我有一个问题,可能很简单,我有一个带有样式化单元格和xaml数据触发器的数据网格:

    <Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="ToolTip" Value="Click to zoom it to hour view for selected day" />
        <Style.Triggers>

            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" Value="X ">
                <Setter Property="Background" Value="#FF6666" />
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="HorizontalAlignment" Value="Center" />
            </DataTrigger>

            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" Value="X">
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="HorizontalAlignment" Value="Center" />
            </DataTrigger>

        </Style.Triggers>
    </Style>

Well fairly simple, it does what it should when value is X it centres text and gives it proper background. 相当简单,当值是X时它将执行应做的事情,它将文本居中并为其提供适当的背景。 But this background is a problem. 但是这种背景是个问题。

I want background fit the cell width, as now cell content text background is coloured as text width, height plus margins but does not fill whole cell. 我希望背景适合单元格的宽度,因为现在单元格内容文本背景的颜色为文本宽度,高度和边距,但不能填充整个单元格。 Can it be done some easy way?? 可以通过一些简单的方法吗?

I tried adding: 我尝试添加:

<Setter Property="Width" Value="{Binding ActualWidth, ElementName=parentElementName}" /> 

to data trigger but no luck. 触发数据,但没有运气。

I don't think setting VerticalAlignment/HorizontalAlignment for DataGridCell is a good idea, since it produces some weird effects. 我认为为DataGridCell设置VerticalAlignment / Horizo​​ntalAlignment不是一个好主意,因为它会产生一些奇怪的效果。 VerticalContentAlignment/HorizontalContentAlignment sounds better but unfortunately DataGridCell ignores them. VerticalContentAlignment / Horizo​​ntalContentAlignment听起来更好,但不幸的是DataGridCell忽略了它们。

DataGridCell content can be centered via ElementStyle of each column where element style (in this case TextBlock) changes alignment, eg: DataGridCell内容可以通过元素样式(在本例中为TextBlock)更改对齐方式的每一列的ElementStyle居中,例如:

<DataGrid RowHeight="40">
<DataGrid.Resources>
    <Style x:Key="txt" TargetType="TextBlock">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
</DataGrid.Resources>
<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="Background" Value="#FF6666" />
    </Style>
</DataGrid.CellStyle>

<DataGrid.Columns>
    <DataGridTextColumn ElementStyle="{StaticResource txt}" 
                        Binding="{Binding Path=Name}" Header="Name" />
</DataGrid.Columns>
</DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM