从要设置样式的实例的属性中获取样式的值

托布森

我想在运行时创建元素并为其添加样式。

给定以下Xaml,我想将属性的值绑定Center到样式对象的实际值(请参阅main方法)。我尝试了不同的Binding表示法,但是没有成功(可能是因为我对xaml还是陌生的)。我也尝试只更改Center实例上的实例,但是实例被冻结,无法更改。

<Window x:Name="window" x:Class="CirclePing.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">
    <Window.Resources>
        <Style x:Key="AlertBubble" TargetType="{x:Type Path}">
            <Setter Property="StrokeThickness" Value="0"/>
            <Setter Property="Data">
                <Setter.Value>

                    <!-- how can I bind Center to the Tag property of a 'styled' instance? -->
                    <EllipseGeometry x:Name="circle"
                                Center="200,200" 
                                RadiusX="100" 
                                RadiusY="100">
                    </EllipseGeometry>

                </Setter.Value>
            </Setter>
            <Setter Property="Fill">
                <Setter.Value>
                    <RadialGradientBrush>
                        <GradientStop Color="Black"/>
                        <GradientStop Color="#2F5CB2"/>
                    </RadialGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="OpacityMask">
                <Setter.Value>
                    <RadialGradientBrush>
                        <GradientStop Color="#00000000" Offset="0.5"/>
                        <GradientStop Color="#FFFFFFFF" Offset="1"/>
                    </RadialGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
</Window>

我的Main方法:

    InitializeComponent();

    Random r = new Random();
    for (int i = 0; i < 500; i++)
    {
        var path = new Path();

        int positionX = r.Next(1400);
        int positionY = r.Next(800);
        path.Tag = new Point(positionX, positionY);

        path.Style = (Style)this.Resources["AlertBubble"]; 
     }

现在,我希望做到以下几点:(EllipseGeometry)path.Data).Center.Y == positionY我应该使用什么绑定表达式?

罗希特·瓦茨(Rohit Vats)

您可以使用RelativeSource标记扩展名绑定Center并找到父Path实例对象,如下所示:

<EllipseGeometry x:Name="circle"
                 Center="{Binding Tag, RelativeSource={RelativeSource 
                                       Mode=FindAncestor, AncestorType=Path}}" 
                 RadiusX="100" 
                 RadiusY="100"/>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过javascript / jQuery从CSS类获取样式属性?

获取样式值的数字,而无需输入“ px;” 后缀

如何在IE9剥离样式属性之前获取样式属性值

在PyGObject中设置样式属性

从组件的“样式”属性获取CSS属性值

根据ViewModel中存在的属性值在ItemContainerStyleSelector中设置样式

Scrapy如何提取样式属性?

为什么要获取节点元素的样式属性在javascript中返回false?

使用DataTemplates从ListView中的Selecteditem获取样式

使用XPath获取样式值大于阈值的元素

如何从点击事件目标中获取样式?

无法从React中的event.target获取样式

从值中获取样式化的枚举名称

如何获取样式表中定义的名称空间前缀,而不是从输入XML中获取

如何从其隐藏的输入字段中获取样式为块和值的div

POI Excel:获取样式名称

使用jQuery获取样式化表单的单选按钮值

在Windows Phone 8.1的样式中设置多属性值的值

使用css()获取样式属性值

如何使用ruby获取样式的值-“宽度”的百分比?

从html字符串中使用正则表达式获取样式属性

bash中的C ++文件读取样式

如何从angular2的jQuery中获取样式

如何从带有angular2和Typescript的HTMLElement获取样式值

从 css、JavaScript 获取样式

如何在javascript中获取样式变换旋转值?

jQuery Select2 - 从原始选项的选择中获取样式属性

如何从 Laravel 8 中的数据库中获取样式元素

如何在 Python 中的 div 标签中获取样式值?