基于触发器的XAML中的通用格式和对齐

时间:2017-10-17 09:35:13

标签: c# wpf xaml

我正在尝试在我的应用程序上应用整体格式。该应用程序已经构建。在数据网格中显示数据时,我想格式化并右对齐所有包含数量的文本块。这是可以使用某种触发器或任何其他方式吗?是否可以知道绑定路径是否包含单词“amount”以及是否应用了所需的格式。

基本上我想根据整个应用中的值从一个位置控制格式和对齐。因此,长期运行很容易改变。

以下是我的XAML视图:

<UserControl x:Class="CPOSApplication.UserControls.Sales.SalesFilterResult"
             xmlns:PaginationControl="clr-namespace:CPOSApplication.GeneralControl.PaginationControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:localConvertor="clr-namespace:CPOSApplication.Convertors"
             xmlns:com="pagina"
             xmlns:local="clr-namespace:CPOSApplication.Convertors"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="1000">
    <UserControl.Resources>
        <local:DataGridRowToIndexConvertor x:Key="RowToIndexConvertor">

        </local:DataGridRowToIndexConvertor>
        <localConvertor:PermissionToEnableConvertor x:Key="PermissionToBoolConvertor"/>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="4*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label x:Name="GridLabel"  Grid.Row="0"   Style="{DynamicResource HeadingLabelsCustomStyle}" Content="Sales"/>
        <DataGrid Grid.Row="1" Style="{DynamicResource DataGridStyle}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle}" RowStyle="{DynamicResource DataGridRowStyle}" CellStyle="{DynamicResource DataGridCellStyle}" x:Name="SalesGrid">
            <DataGrid.Columns>
                <DataGridTemplateColumn  Header="Id" Visibility="Collapsed" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Id}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Index" Width="*"  IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock x:Name="IndexCell" Style="{DynamicResource GridTextBlock}" Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource RowToIndexConvertor}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Serial Number" Width="*" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=SerialNumber}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Customer" Width="*" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=CustomerName}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Payment Method" Width="*" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=FormattedPaymentType}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Sale Date" Width="*" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=CreatedDate}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Sale Amount" Width="*" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=TotalAmount}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="*" Header="Actions" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Style="{DynamicResource ActionPanel}">
                                <Image  x:Name="EditImg" IsEnabled="{Binding Converter={StaticResource PermissionToBoolConvertor}, ConverterParameter='Edit Sale|Sale'}" Visibility="{Binding Converter={StaticResource LiveConnectionToVis}}" Style="{DynamicResource EditImg}" Source="/CPOSApplication;component/Resources/Images/Icons/edit.png" Tag="{Binding}" MouseDown="Edit_Click"></Image>
                                <Image  x:Name="DeleteImg" IsEnabled="{Binding Converter={StaticResource PermissionToBoolConvertor}, ConverterParameter='Delete Sale|Sale'}" Visibility="{Binding Converter={StaticResource LiveConnectionToVis}}" Style="{DynamicResource DeleteImg}" Source="/CPOSApplication;component/Resources/Images/Icons/delete.png" Tag="{Binding}" MouseDown="Delete_Click"></Image>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <PaginationControl:Paginator Background="#e4e4e4" x:Name="pager" Grid.Row="2" VerticalAlignment="Bottom"  Margin="0,10,0,0"  Height="30" />
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:-1)

您可以尝试绑定TextBlocks,如下所示:

<TextBlock Text="{Binding Path=TotalAmount}" TextAlignment="{Binding Path=TotalAmount, Converter="{StaticResource ContentToAlignmentConverter}" />

您必须自己实现ContentToAlignmentConverter。我不会在这里放一个代码,因为这取决于你想要检查它的内容,但它自己很容易做到。 Here is a sample of a value converter in WPF

修改

如果你想对DataGrid中的所有TextBlocks应用这些更改,你应该尝试这样的事情(不确定它是否会起作用,但应该如此):

<DataGrid.Resources>
    <Style TargetType="{x:Type TextBlock}" BasedOn="{DynamicResource GridTextBlock}">
        <Setter Property="TextAlignment" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text, Converter="{StaticResource ContentToAlignmentConverter}}" />
    </Style>
</DataGrid.Resources>