0

我有一个 DataGridColumnHeader 样式,它有一个位于 DataGridColumn 上方的 TextBox。在 TextBox 样式中,有一个 DataTrigger,它根据 DataGridColumn 中的文本设置其可见性。我不想检查它的值是否等于“Pos”,而是想使用一个转换器来查看它的值是否以字符串“Pos”开头。但是当我添加转换器时,转换器中的 value 参数结果不是字符串。相反,它是数据网格所属的 MainWindow 对象。为什么当我添加转换器时它会具有不同的含义?

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderStyle"
            xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
    <Style.Resources>
        <SolidColorBrush x:Key="borderBackground">#E4E5ED</SolidColorBrush>
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Border Background="{StaticResource borderBackground}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <TextBox Margin="1" Grid.Column="0" Grid.Row="0" IsReadOnly="True"
                                Text="{Binding Path=DataContext.TotalPos, StringFormat=N0, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}">
                            <TextBox.Style>
                                <Style TargetType="TextBox">
                                    <Setter Property="Visibility" Value="Hidden"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding}" Value="Pos">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>

                        <theme:DataGridHeaderBorder Grid.Column="0" Grid.Row="1" SortDirection="{TemplateBinding SortDirection}"
                                 IsHovered="{TemplateBinding IsMouseOver}"
                                 IsPressed="{TemplateBinding IsPressed}"
                                 IsClickable="{TemplateBinding CanUserSort}"
                                 Background="{TemplateBinding Background}"
                                 BorderBrush="{TemplateBinding BorderBrush}"
                                 BorderThickness="{TemplateBinding BorderThickness}"
                                 Padding ="{TemplateBinding Padding}"
                                 SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
                                 SeparatorBrush="{TemplateBinding SeparatorBrush}">

                            <TextBlock Grid.Column="0" Grid.Row="1"  Text="{TemplateBinding  Content}" 
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                   TextWrapping="Wrap"></TextBlock>

                        </theme:DataGridHeaderBorder>
                        <Thumb Grid.Column="0" Grid.Row="1" Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                        <Thumb Grid.Column="0" Grid.Row="1" Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

带转换器:

<src:StartsWith x:Key="startsWith"/>
...
<Style TargetType="TextBox">
    <Setter Property="Visibility" Value="Hidden"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Converter={StaticResource startsWith}, ConverterParameter=Pos}" Value="True">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
...

[ValueConversion(typeof(string), typeof(bool))]
public class StartsWith : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((string)value).StartsWith((string)parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("Not Implemented.");
    }
}
4

2 回答 2

0

我绑定这个,谢谢

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridColumnHeader, AncestorLevel=1}, Path=Column.Header, Converter={StaticResource startsWith}, ConverterParameter=Pos}" Value="True">
于 2013-10-14T20:47:08.810 回答
0

它不知道要绑定什么...Binding RelativeSource={RelativeSource Self}, Path=Text为转换器添加。

于 2013-10-14T20:39:19.497 回答