简体   繁体   English

根据属性值更改样式

[英]Change style based on property value

On a Canvas of the main window I'm drawing some sensors.在主 window 的Canvas上,我正在绘制一些传感器。

I created the class "Sensor"我创建了 class “传感器”

public class Sensor
{
    public int Id_Sens { get; set; }    
    public int Type { get; set; }
    public UIElement Ui_Element { get; set; }
}

Programmatically I create a Grid with two columns (a Rectangle with the same WIDTH and HEIGHT (-> become a square), with the following Style, in the first one and a Label in the second).以编程方式,我创建了一个包含两列的Grid (一个具有相同宽度和高度的Rectangle (-> 变成一个正方形),第一个具有以下样式,第二个是Label )。 I associate this Grid with the Ui_Element ( Ui_Element = gridMain ).我将此GridUi_Element ( Ui_Element = gridMain ) 相关联。

This works fine.这工作正常。

Based on the value of the property Type , I want to change the radius of the Rectangle (and draw a circle)根据属性Type的值,我想改变Rectangle的半径(并画一个圆)

I created the follow XAML我创建了以下 XAML

<Style x:Key="Sens_Shape" TargetType="{x:Type Rectangle}">
    <Setter Property="Stroke" Value="Black"/>
    <Setter Property="StrokeThickness" Value="1"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Type}" Value="0">
            <Setter Property="RadiusX" Value="60"/>
            <Setter Property="RadiusY" Value="60"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

How do I bind the property Type of the class Sensor to the trigger?如何将 class Sensor的属性Type绑定到触发器?

EDIT I solved with the points 1 and 3 of of EldHasp编辑我用 EldHasp 的第 1 点和第 3 点解决了

I may not give an exact answer, because there are not enough details in the original question.我可能不会给出确切的答案,因为原始问题中没有足够的细节。

  1. The element that you show in the Window is recorded in the property public UIElement Ui_Element ?您在 Window 中显示的元素记录在属性public UIElement Ui_Element
    If so, then change the property type to public FrameworkElement Ui_Element , since the Style and DataContext properties are defined on the FrameworkElement, not UIElement.如果是这样,则将属性类型更改为public FrameworkElement Ui_Element ,因为 Style 和 DataContext 属性是在 FrameworkElement 上定义的,而不是在 UIElement 上定义的。

  2. If it is a narrower type (for example, it is always Shape as in your examples), then it is even better to specify this narrower type.如果它是较窄的类型(例如,在您的示例中始终是 Shape ),那么最好指定这种较窄的类型。

  3. If the style shown by you is the style of the element in the Ui_Element property, then it is enough to pass the Sensor into the Data Context of this element.如果您显示的样式是 Ui_Element 属性中元素的样式,那么将 Sensor 传递到该元素的 Data Context 中就足够了。
    Example: sensor.Ui_Element.DataContext = sensor;示例: sensor.Ui_Element.DataContext = sensor; . .

  4. You are very vainly creating UI elements on Sharpe.您在 Sharpe 上创建 UI 元素非常徒劳。 The core language of WPF is XAML. WPF的核心语言是XAML。 And in very rare, rather difficult cases, it is worth using Sharp instead.在非常罕见、相当困难的情况下,值得使用 Sharp 代替。 In most cases, a XAML solution will be more simpler, more typical, more understandable than Sharpe.在大多数情况下,XAML 解决方案将比 Sharpe 更简单、更典型、更易于理解。
    In your case, you should create a Sensor's collection without the Ui_Element property.在您的情况下,您应该创建一个没有 Ui_Element 属性的传感器集合。 Create a template selector for the Sensor which, by the Type property, will set its visual appearance.为 Sensor 创建一个模板选择器,通过 Type 属性设置其视觉外观。 In XAML, set the ItemsControl for this collection and give it a template selector.在 XAML 中,为此集合设置 ItemsControl 并为其提供模板选择器。
    Besides there are several other typical WPF implementations.此外还有其他几个典型的 WPF 实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM