首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使ProgressRing在MahApps.Metro中变小

使ProgressRing在MahApps.Metro中变小
EN

Stack Overflow用户
提问于 2013-07-21 16:01:46
回答 1查看 6.8K关注 0票数 7

看起来,MahApps.Metro ProgressRing控件的最小大小为60x60。

ProgressRing有一个名为"IsLarge“的属性,但即使将其设置为"False”,也似乎对能够使ProgressRing小于60x60没有任何影响。

显然,改变高度和宽度属性也不影响这一点。

将GitHUb看作是ProgressRing的实际c#代码,似乎有几个属性会影响椭圆直径等,但这些属性是私有属性,不能通过外部调用来设置。

我怎么才能把这个变小呢?比如说20x20还是30x30?

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

代码语言:javascript
复制
<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“文件的代码片段

代码语言:javascript
复制
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();
        }

EDIT:MainWindow.xaml

代码语言:javascript
复制
<Grid>
    <Controls:ProgressRing x:Name="PRing" IsLarge="False" MinHeight="15" MinWidth="15" Height="15" Width="15"></Controls:ProgressRing>
</Grid>

EDIT:MainWindow.xaml.cs

代码语言:javascript
复制
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PRing.EllipseDiameter = 5;
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-21 16:19:05

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

代码语言:javascript
复制
<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"/>

...

默认情况下,有WidthHeight of 60。据我所知,易集WidthHeight只直接影响椭圆。

EDIT:

让戒指更小的是,您可以通过代码来处理参数EllipseDiameterEllipseOffset,因为它们是不可用的(私有的)。

代码语言:javascript
复制
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设置器的属性

代码语言:javascript
复制
public double EllipseDiameter
{
    get 
    {
        return (double)GetValue(EllipseDiameterProperty); 
    }

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

SetEllipseDiameter中,正在检查Ellipse的大小,如果Width小于60,则设置为6.0。我们评论一下。

代码语言:javascript
复制
private void SetEllipseDiameter(double width)
{
    //if (width <= 60)
    //{
    //    EllipseDiameter = 6.0;
    //}
    //else
    //{
    //    EllipseDiameter = width * 0.1 + 6;
    //}
 }

Style中,Ellipse的直径是这样的:

代码语言:javascript
复制
<Setter Property="MinHeight" Value="30"/>
<Setter Property="MinWidth" Value="30"/>
<Setter Property="EllipseDiameter" Value="3.0" />

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

代码语言:javascript
复制
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);
    }
}

使用这些参数伪造这些操作,您可以配置WidthHeightProgressRing控件。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17774185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档