【问题标题】:Make the ProgressRing in MahApps.Metro Smaller使 MahApps.Metro 中的 ProgressRing 更小
【发布时间】:2013-07-21 16:01:46
【问题描述】:

看起来 MahApps.Metro ProgressRing 控件默认的最小尺寸为 60x60。

ProgressRing 有一个名为“IsLarge”的属性,但即使将其设置为“False”,它似乎也不会影响 ProgressRing 小于 60x60。

显然更改 Height 和 Width 属性也不会影响这一点。

看Github作为ProgressRing的实际c#代码,貌似有几个属性影响椭圆直径等,但这些属性是私有属性,不能从外部调用设置。

我怎样才能把它变小?说 20x20 还是 30x30?

在下面的代码中,我指定 IsLarge=False,并将大小设置为 30x30,但仍默认为 60x60。

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Orange.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
         <Grid>
            <Controls:ProgressRing IsActive="True" IsLarge="False" Height="30" Width="30"></Controls:ProgressRing>
        </Grid>
</Window>

下面是来自GitHub - MahApps.Metro上的“ProgressRing.cs”文件中的sn-ps代码

namespace MahApps.Metro.Controls
{
    [TemplateVisualState(Name = "Large", GroupName = "SizeStates")]
    [TemplateVisualState(Name = "Small", GroupName = "SizeStates")]
    [TemplateVisualState(Name = "Inactive", GroupName = "ActiveStates")]
    [TemplateVisualState(Name = "Active", GroupName = "ActiveStates")]
    public class ProgressRing : Control


        private void SetMaxSideLength(double width)
        {
            MaxSideLength = width <= 60 ? 60.0 : width;
        }

        private void SetEllipseDiameter(double width)
        {
            if (width <= 60)
            {
                EllipseDiameter = 6.0;
            }
            else
            {
                EllipseDiameter = width * 0.1 + 6;
            }
        }

        private void UpdateLargeState()
        {
            Action action;

            if (IsLarge)
                action = () => VisualStateManager.GoToState(this, "Large", true);
            else
                action = () => VisualStateManager.GoToState(this, "Small", true);

            if (_deferredActions != null)
                _deferredActions.Add(action);

            else
                action();
        }

编辑:MainWindow.xaml

<Grid>
    <Controls:ProgressRing x:Name="PRing" IsLarge="False" MinHeight="15" MinWidth="15" Height="15" Width="15"></Controls:ProgressRing>
</Grid>

编辑:MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PRing.EllipseDiameter = 5;
        }
    }

【问题讨论】:

  • 设置高度和宽度对我有用!设置两个属性,而不仅仅是一个!

标签: c# wpf mahapps.metro


【解决方案1】:

您必须为ProgressRing 找到一种样式,并为自己设置WidthHeight。对我来说,样式位于:MahApps.Metro master \ MahApps.Metro \ Themes \ ProgressRing.xaml:

<Style TargetType="{x:Type Controls:ProgressRing}">
    <Setter Property="Foreground" Value="{DynamicResource AccentColorBrush}"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="MinHeight" Value="30"/>
    <Setter Property="MinWidth" Value="30"/>

...

默认有WidthHeight60。据我了解,简单设置WidthHeight 直接影响椭圆。

EDIT:

什么会使环更小,您可以通过代码使用参数EllipseDiameterEllipseOffset,因为 XAML 它们将不可用(作为 private)。 p>

private void SetEllipseDiameter(double width)
{
    if (width <= 60)
    {
        EllipseDiameter = 3.0; // as default 6.0
    }
    else
    {
        EllipseDiameter = width * 0.1 + 6;
    }
}

private void SetEllipseOffset(double width)
{
    if (width <= 60)
    {
        EllipseOffset = new Thickness(0, 12, 0, 0); // as default 24
    }
    else
    {
        EllipseOffset = new Thickness(0, width * 0.4 + 12, 0, 0);
    }
}

EDIT2:

要设置Ellipse 的直径可以如下进行。我们确实有属性 EllipseDiameter setter public:

public double EllipseDiameter
{
    get 
    {
        return (double)GetValue(EllipseDiameterProperty); 
    }

    set // default as private
    {
        SetValue(EllipseDiameterProperty, value);
    }
}

SetEllipseDiameter 中检查Ellipse 的大小,如果Width 小于60,则设置为6.0。我们注释掉。

private void SetEllipseDiameter(double width)
{
    //if (width <= 60)
    //{
    //    EllipseDiameter = 6.0;
    //}
    //else
    //{
    //    EllipseDiameter = width * 0.1 + 6;
    //}
 }

Style 中设置Ellipse 的直径,如下所示:

<Setter Property="MinHeight" Value="30"/>
<Setter Property="MinWidth" Value="30"/>
<Setter Property="EllipseDiameter" Value="3.0" />

EllipseOffset 也是如此。他也是,一开始私有,并且检查了小于 60 的Width

private void SetEllipseOffset(double width)
{
    // you can drop this check
    if (width <= 60)
    {
        EllipseOffset = new Thickness(0, 24, 0, 0); 
    }
    else
    {
        EllipseOffset = new Thickness(0, width * 0.4 + 24, 0, 0);
    }
}

用这些参数锻造这些操作,可以配置ProgressRing控件的WidthHeight

【讨论】:

  • 设置 MinHeight 和 MinWidth 似乎可行,但现在限制似乎是 30x30!如果我尝试比它更小它保持不变,我猜是因为椭圆中的圆的直径必须有一些最小高度/宽度? ;-)
  • 此外,我也尝试将高度和宽度以及最小高度和最小宽度设置为 20x20,但仍然保持在 30x30。
  • @J P:你需要减小椭圆的宽度/高度吗?或者设置宽度/高度小于30x30?如果少,那是多少?
  • 我希望整个控件看起来小于 30x30,但更改 minheight 和 minwidth 值似乎在 30 以下不起作用。这意味着它不会小于 30x30。我假设这是因为椭圆直径值也有最小要求,如果您将进度环设置为小于 30x30,它将无法正确显示。我尝试通过代码访问 EllipseDiameter 属性,正如您在上面编辑的帖子中看到的那样,我收到此错误:无法将属性或索引器“MahApps.Metro.Controls.ProgressRing.EllipseDiameter”分配给 - 它是只读的跨度>
  • @JP:奇怪的是,它对我有用。我对此进行了测试:ProgressRing 的样式和控制代码已放入我的项目中。他们不是read only。它们是private,只能设置在内部:private set { SetValue(EllipseDiameterProperty, value); }。如果你愿意,我可以给你项目的链接。
猜你喜欢
  • 2020-01-28
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多