RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 581500
Accepted
Denisok
Denisok
Asked:2020-10-23 03:08:30 +0000 UTC2020-10-23 03:08:30 +0000 UTC 2020-10-23 03:08:30 +0000 UTC

如何实时转换xaml标记?

  • 772

我有一个 Page.Resources,假设其中绘制了按钮元素的大小。怎样才能让应用程序窗口在伸缩时,按钮的比例同时变化。例如,按钮图标始终是应用程序窗口宽度的 1/4。

<Style x:Key="Autorize_Button" TargetType="Button">
        <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
        <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
        <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
        <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
        <Setter Property="Padding" Value="8,4,8,4"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
        <Setter Property="UseSystemFocusVisuals" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <Rectangle Name="Line" Height="1.5" Width="{TemplateBinding ActualWidth}" Fill="White" Opacity="0" VerticalAlignment="Bottom" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

下面是按钮的资源,需要改变叫Line的矩形的宽度(窗口宽度的1/3)

c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    VladD
    2020-10-23T03:31:10Z2020-10-23T03:31:10Z

    不幸的是,ActualWidth在 UWP 中,与 WPF 不同,它是一个计算属性,并且不会发送有关其更改的消息。因此,我们必须做出一个相当复杂的决定。

    首先,我们需要DependencyProperty,它将给出当前宽度的值。在页面类中(让它被调用MainPage)我们声明:

    public double ComputedActualWidth
    {
        get { return (double)GetValue(ComputedActualWidthProperty); }
        set { SetValue(ComputedActualWidthProperty, value); }
    }
    
    public static readonly DependencyProperty ComputedActualWidthProperty =
        DependencyProperty.Register("ComputedActualWidth", typeof(double), typeof(MainPage),
                                    new PropertyMetadata(0.0));
    

    并在构造函数中

    public MainPage()
    {
        InitializeComponent();
        SizeChanged += (o, args) => ComputedActualWidth = ActualWidth;
        ComputedActualWidth = ActualWidth;
    }
    

    现在你可以绑定:

    <Rectangle Name="Line" Height="1.5"
               Width="{Binding ComputedActualWidth,
                               ElementName=Root,
                               Converter={StaticResource OneFourthConverter}}"
               ... />
    

    将转换器声明为

    class OneFourthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object p, string language) =>
            (double)value / 4;
    
        public object ConvertBack(object value, Type targetType, object p, string language) =>
            (double)value * 4;
    }
    

    并投入可用资源:

    <Page.Resources>
        <local:OneFourthConverter x:Key="OneFourthConverter"/>
    </Page.Resources>
    

    为页面命名Root。


    从工作代码中提取:

    <Page
        x:Class="Test.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test"
        xmlns:i="using:Microsoft.Xaml.Interactivity"
        xmlns:core="using:Microsoft.Xaml.Interactions.Core"
        xmlns:media="using:Microsoft.Xaml.Interactions.Media"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Name="Root">
        <Page.Resources>
            <local:OneFourthConverter x:Key="OneFourthConverter"/>
            <Style x:Key="Autorize_Button" TargetType="Button">
                <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
                <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
                <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
                <Setter Property="Padding" Value="8,4,8,4"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
                <Setter Property="UseSystemFocusVisuals" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                                <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <Rectangle Name="Line" Height="15"
                                           Width="{Binding ComputedActualWidth,
                                                           ElementName=Root,
                                                           Converter={StaticResource OneFourthConverter},
                                                           Mode=OneWay}"
                                           Fill="Red" Opacity="1" VerticalAlignment="Bottom"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Page.Resources>
        <Grid Background="Gray">
            <Button Style="{StaticResource Autorize_Button}" Height="30" Width="300"/>
        </Grid>
    </Page>
    

    我将不透明度更改为 1 以使线条可见。

    • 4

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5