首页 > 解决方案 > WPF XAML 数据触发器内容模板

问题描述

我正在使用带有 DataTriggers 的 TargetType“ContentPresenter”样式来动态分配 ContentTemplate。

<Style x:Key="styleContentValeur" TargetType="{x:Type ContentPresenter}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="chaine">
            <Setter Property="ContentTemplate" Value="{StaticResource dataChaine}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="chainemultiligne">
            <Setter Property="ContentTemplate" Value="{StaticResource dataChaineMultiligne}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="entier">
            <Setter Property="ContentTemplate" Value="{StaticResource dataEntier}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="reel">
            <Setter Property="ContentTemplate" Value="{StaticResource dataReel}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="booleen">
            <Setter Property="ContentTemplate" Value="{StaticResource dataBooleen}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="liste">
            <Setter Property="ContentTemplate" Value="{StaticResource dataListe}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="listedynamique">
            <Setter Property="ContentTemplate" Value="{StaticResource dataListe}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="date">
            <Setter Property="ContentTemplate" Value="{StaticResource dataDate}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding TAT_LIBELLE, Mode=OneWay}" Value="grammaire">
            <Setter Property="ContentTemplate" Value="{StaticResource dataChaine}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

数据模板是这样的:

<DataTemplate x:Key="dataChaine">
    <local:TextBoxPerso Style="{StaticResource styleTextBox}"
                        HorizontalContentAlignment="Left" VerticalContentAlignment="Center"
                        TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False"
                        oAttribut="{Binding}"
                        Text="{Binding ATT_VALEUR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

<DataTemplate x:Key="dataChaineMultiligne">
    <local:TextBoxPerso  Margin="3" 
                         Style="{StaticResource styleTextBox}"
                         HorizontalContentAlignment="Left" VerticalContentAlignment="Top"
                         TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="False"
                         HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden"
                         oAttribut="{Binding}"
                         Text="{Binding ATT_VALEUR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

<DataTemplate x:Key="dataEntier">
    <local:TextBoxPerso  Style="{StaticResource styleTextBox}"
                         HorizontalContentAlignment="Left" VerticalContentAlignment="Center"
                         TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False"
                         oAttribut="{Binding}"
                         Text="{Binding ATT_VALEUR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

<DataTemplate x:Key="dataReel">
    <local:TextBoxPerso  Style="{StaticResource styleTextBox}"
                         HorizontalContentAlignment="Right" VerticalContentAlignment="Center"
                         TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False"
                         oAttribut="{Binding}"
                         Text="{Binding ATT_VALEUR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

<DataTemplate x:Key="dataBooleen">
    <local:CheckBoxPerso FocusVisualStyle="{x:Null}" IsChecked="{Binding ATT_VALEUR, Converter={StaticResource StringToBoolConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

<DataTemplate x:Key="dataDate">
    <DatePicker IsTodayHighlighted="False" SelectedDate="{Binding ATT_VALEUR, Converter={StaticResource StringToDateTimeConverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

<DataTemplate x:Key="dataListe">
    <local:ComboBoxPerso IsEditable="True"
                         ItemsSource="{Binding tValeur}" DisplayMemberPath="ALV_VALEUR" SelectedValuePath="ALV_ID"
                         SelectedValue="{Binding ATT_VALEUR, Converter={StaticResource StringToIntConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>

所以根据 TAT_LIBELLE 的值,这会选择正确的 ContentTemplate。

我对 DataTemplate 有疑问:“dataDate”。SelectedDate "ATT_VALUE" 的值对应于我的源代码的另一行,其值为 TAT_LIBELLE != "date"。我发现了这一点,因为我在转换器中有一个异常:StringToDateTimeConverter。

这怎么可能?因为通常只有我的源中具有 TAT_LIBELLE = "date" 的行使用这个 ContentTemplate !?

我没有使用正确的方法来动态管理我的 ContentTemplate?

谢谢你。

PS:对不起我的英语...

标签: c#wpfxamldatatriggercontenttemplate

解决方案


推荐阅读