ControlTemplate.Trigger在CellValuePresenter(TargetType)上不起作用



我正在使用Infrastics 2014.2。我有一个XamDataGrid,它用动态列显示数据,网格按其默认样式格式化,此外,一些列的对齐方式设置为"右"(十进制列),上面有一些值掩码,其余列的对齐设置为"左"。

我想删除XamDataGrid的默认CellValuePresenter样式,并为每个单元格编写一个只有TextBlock/ContentPresenter而不是CellValuePresentor的新样式,在应用新样式后,单元格的对齐和掩码应保持不变。应用这种新样式的原因是我希望同一网格的副本在另一个窗口上显示它,而不需要数据操作

你能看看下面的代码吗?我试图实现同样的效果(即使其中有很多更改),但它似乎无论如何都不起作用。

    <Style x:Key="PreviewCellValuePresenterStyle" TargetType="{x:Type igDP:CellValuePresenter}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="CellValuePresenterControlTemplate" TargetType="{x:Type igDP:CellValuePresenter}">
                    <ContentPresenter x:Name="ContentValuePresenter"
                                     VerticalAlignment="Center"
                                     ContentSource="Value"
                                     Margin="4,3,4,4"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HorizontalAlignment" Value="Right">
                            <Setter TargetName="ContentValuePresenter" Property="TextBlock.TextAlignment" Value="Right"></Setter>
                        </Trigger>
                        <Trigger Property="HorizontalContentAlignment" Value="Right">
                            <Setter TargetName="ContentValuePresenter" Property="TextBlock.TextAlignment" Value="Right"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在,我只是为测试设置对齐属性。在上面的代码中,我们尝试用TextBlock代替ContentPresenter,但触发器从未被调用。我不知道我做错了什么。

你能帮我摆脱困境吗?

感谢

If your intention of using text block is only for alignment/appearance, then you can just use setters to set these values. 
            <Style x:Key="PreviewCellValuePresenterStyle" TargetType="{x:Type igDP:CellValuePresenter}" >
                    <Setter Property="FontWeight" Value="DemiBold"/>
                    <Setter Property="Height" Value="24"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="VerticalContentAlignment" Value="Top"/>
             </Style>
But, If you need some other implementations like value masking, or need text block to be there, 

 <Style x:Key="PreviewCellValuePresenterStyle" TargetType="{x:Type igDP:CellValuePresenter}">
                                                    <Setter Property="Template">
                                                        <Setter.Value>
                                                            <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                                                                <TextBlock HorizontalAlignment="Center" Style="{DynamicResource TextStyle}">
                                                                </TextBlock>
                                                            </ControlTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </Style>

    <Style x:Key="TextStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
      </Style>

相关内容

  • 没有找到相关文章

最新更新