如何重置DataTemplate的ThemeResource?

时间:2017-01-05 21:28:12

标签: c# uwp uwp-xaml

我已经定义了一个包含文本框的DataTemplate。在"手套模式"我需要一个大字体/ minHeight,所以触摸屏工作得很好,但在"办公模式"我想要不同的价值观。我相信这应该是可能的,但无法弄清楚。

如何修改后面代码中的主题?或者,如果这是完全错误的,我应该怎么做?

样式:

<Style x:Key="GloveTextBoxStyle" TargetType="TextBox">
    <Setter Property="FontSize" Value="30"/>
    <Setter Property="MinHeight" Value="60"/>
</Style>
<Style x:Key="OfficeTextBoxStyle" TargetType="TextBox">
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="MinHeight" Value="30"/>
</Style>

的DataTemplate:

<DataTemplate x:Key="InspectionItemStringTemplate" x:DataType="data:InspectionItem"><TextBox Text="{x:Bind NewValue,Mode=TwoWay}"
                     x:Name="MyTextBox" 
                     x:Phase="1" Style="{ThemeResource GloveTextBoxStyle}"/></DataTemplate>

1 个答案:

答案 0 :(得分:2)

IValueConverter怎么样? 你可以创建类似的东西:

public class TextBoxStyleConverter : IValueConverter
{
    public Style GloveTextBoxStyle { get; set; }

    public Style OfficeTextBoxStyle { get; set; }

    public object Convert(object value, Type targetType, object parameter, string language)
    {    
        // analyze binded value and return needed style
        return condition ? GloveTextBoxStyle : OfficeTextBoxStyle;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

并在您的XAML代码中

    <local:TextBoxStyleConverter x:Key="TextBoxStyleConverter">
        <local:TextBoxStyleConverter.GloveTextBoxStyle>
            <Style TargetType="TextBox">
                <Setter Property="FontSize" Value="30"/>
                <Setter Property="MinHeight" Value="60"/>
            </Style>
        </local:TextBoxStyleConverter.GloveTextBoxStyle>
        <local:TextBoxStyleConverter.OfficeTextBoxStyle>
            <Style TargetType="TextBox">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="MinHeight" Value="30"/>
            </Style>
        </local:TextBoxStyleConverter.OfficeTextBoxStyle>
    </local:TextBoxStyleConverter>


    <DataTemplate x:Key="InspectionItemStringTemplate"
                  x:DataType="data:InspectionItem">
        <TextBox Text="{x:Bind NewValue,Mode=TwoWay}"
                 x:Name="MyTextBox" 
                 x:Phase="1"
                 Style="{Binding Converter={StaticResource TextBoxStyleConverter}}"/>
    </DataTemplate>