Как изменить размер многоугольника в WPF? - PullRequest
2 голосов
/ 07 июля 2010

Я работаю над своим первым WPF-приложением и тестирую пользовательский элемент управления, необходимый для круга с кнопкой воспроизведения, нарисованной посередине.Я, кажется, ударил немного заминку, хотя.Когда я рисую свою кнопку воспроизведения, я не могу заставить ее изменить размер рядом с кругом.В частности, когда я изменяю размер круга, чтобы он был шире или выше, многоугольник кнопки воспроизведения остается того же размера и в той же абсолютной позиции.Любой указатель на настройку моего XAML или кода для исправления этого?

Существующий XAML:

<Window x:Class="WPFTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WPFTest">
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="my:GradientButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type my:GradientButton}">
                            <Grid>
                                <Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Top" HorizontalAlignment="Left">
                                    <Ellipse.Fill>
                                        <LinearGradientBrush>
                                            <GradientStop Color="{TemplateBinding GradientStart}" Offset="0"></GradientStop>
                                            <GradientStop Color="{TemplateBinding GradientEnd}" Offset="1"></GradientStop>
                                        </LinearGradientBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                                <Polygon Points="{TemplateBinding PlayPoints}" Fill="{TemplateBinding Foreground}" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
        <my:GradientButton Content="Button" Height="50" x:Name="gradientButton1" Width="50" GradientStart="#FFCCCCCC" GradientEnd="#FFAAAAAA" PlayPoints="18,12 35,25 18,38" />
    </StackPanel>
</Window>

Код:

public class GradientButton : Button
    {
        internal static DependencyProperty GradientStartProperty;
        internal static DependencyProperty GradientEndProperty;
        internal static DependencyProperty PlayPointsProperty;

        static GradientButton()
        {
            GradientStartProperty = DependencyProperty.Register("GradientStart", typeof(Color), typeof(GradientButton));
            GradientEndProperty = DependencyProperty.Register("GradientEnd", typeof(Color), typeof(GradientButton));
            PlayPointsProperty = DependencyProperty.Register("PlayPoints", typeof(PointCollection), typeof(GradientButton));
        }

        public Color GradientStart
        {
            get { return (Color)base.GetValue(GradientStartProperty); }
            set { SetValue(GradientStartProperty, value); }
        }

        public Color GradientEnd
        {
            get { return (Color)base.GetValue(GradientEndProperty); }
            set { SetValue(GradientEndProperty, value); }
        }

        public PointCollection PlayPoints
        {
            get
            {
                //this is where I'm trying to make the resizing dynamic, but this property never seems to get hit when I put in a breakpoint?
                PointCollection result = new PointCollection();
                double top = this.Width / 2.778;
                double left = this.Width / 4.167;
                double middle = this.Height / 2.00;
                double right = this.Width / 1.429;
                double bottom = this.Height / 1.316;

                result.Add(new Point(left, top));
                result.Add(new Point(right, middle));
                result.Add(new Point(left, bottom));

                return result;
            }
            set { SetValue(PlayPointsProperty, value); }
        }
    }

1 Ответ

8 голосов
/ 07 июля 2010

Вам необходимо установить свойство Stretch для Polygon равным Uniform

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...