在page.resource中设置width属性

时间:2015-09-24 07:01:47

标签: c# .net xaml uwp

我正在尝试根据设备是PC还是手机(UWP应用程序)设置RichTextBlock的宽度,但是我遇到了问题...

<Page.Resources>
    <DataTemplate x:Key="sectionTemplate">
        <common:RichTextColumns>
            <common:RichTextColumns.ColumnTemplate>
                <DataTemplate>
                    <RichTextBlockOverflow Width="300" Margin="20,0,0,10"/>
                </DataTemplate>
            </common:RichTextColumns.ColumnTemplate>
            <RichTextBlock Margin="10,0,10,10" TextAlignment="Justify" Width="300" TextWrapping="WrapWholeWords">
                <Paragraph>
                    <Run Text="{Binding}" />
                </Paragraph>
            </RichTextBlock>
        </common:RichTextColumns>
    </DataTemplate>
</Page.Resources>

有关最简单方法的建议吗?

1 个答案:

答案 0 :(得分:1)

使用自定义设备系列状态触发器!

Custom state triggers on GitHub

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup>
        <VisualState x:Name="desktop">
            <VisualState.StateTriggers>
                <triggers:DeviceFamilyStateTrigger DeviceFamily="Desktop" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="richTB.Width" Value="300" />
            </VisualState.Setters>
        </VisualState>
        <VisualState x:Name="mobile">
            <VisualState.StateTriggers>
                <triggers:DeviceFamilyStateTrigger DeviceFamily="Mobile" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="richTB.Width" Value="200" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
<VisualStateManager.VisualStateGroups>