【问题标题】:Find element from external style resource and then, change its own style从外部样式资源中查找元素,然后更改自己的样式
【发布时间】:2018-10-01 20:02:53
【问题描述】:

只是出于好奇。举个例子:

首先,在我的代码隐藏中,我创建了三个复选框和三个省略号:

for (int = 0; i < 3; i++){
    CheckBox checkBox = new CheckBox();
    checkBox.Style = (Style)this.FindResource("MyCheckBoxStyle");
    checkBox.Name = "checkBox_" + i;

    Path ellipsePath = new Path();
    ellipsePath.Fill = Brushes.Gray;

    EllipseGeometry ellipseGeometry = new EllipseGeometry();
    ellipseGeometry.Center = new Point(0, 0);
    ellipseGeometry.RadiusX = 2.5;
    ellipseGeometry.RadiusY = 2.5;

    ellipsePath.Data = ellipseGeometry;
    ellipsePath.Name = "ellipse_" + i;
}

现在来自 CheckBox 样式:

<Style x:Key="MyCheckBoxStyle" TargetType="{x:Type CheckBox}">
        <Setter Property="Foreground" Value="Gray"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}"> 
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Background" TargetName="Border" Value="Green"/>
                            <Setter Property="BorderBrush" TargetName="Border" Value="Green"/>
                            <Setter Property="Foreground" Value="Green"/>
                <!-- Assumming the checkbox I'm checking is called "checkBox_1" I want to
                be able to find the ellipse path whose name is "ellipse_1" and change
                its style (for example, its color) -->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这个想法是每个复选框都与一个椭圆相关联。正如评论所说,假设我正在检查的复选框名为checkBox_1,我希望能够找到名称为ellipse_1 的椭圆元素并更改其样式(例如,它的颜色)。

这可能吗?

编辑

从您的回答中,我知道如果复选框和椭圆都属于同一个模板,我可以做到这一点。我发现的问题(对不起,我忘了提)是目前我正在使用一个网格,其中一行包含省略号,另一行包含在运行时创建的复选框(这就是我使用的原因代码隐藏):

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    使用如下所示的 DataTemplate 作为 ItemsControl 的 ItemTemplate:

    <DataTemplate x:Key="itemTemplate">
        <StackPanel Orientation="Horizontal">
            <Path x:Name="path" Fill="Gray" VerticalAlignment="Center" Margin="5">
                <Path.Data>
                    <EllipseGeometry RadiusX="2.5" RadiusY="2.5"/>
                </Path.Data>
            </Path>
            <CheckBox x:Name="checkBox"/>
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding ElementName=checkBox, Path=IsChecked}"
                         Value="True">
                <Setter TargetName="path" Property="Fill" Value="Green"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    

    【讨论】:

    • 我觉得这是个好主意。但是,我在我的问题中添加了详细信息。如果我在运行时创建省略号和复选框,这意味着我应该从代码隐藏中创建例如三个ItemsControls,但是将那些ItemsControls 的样式定义为样式资源?
    • 不,您不应该在代码后面创建任何 UI 元素。相反,您可以创建一个视图模型项类并将 DataTemplate 中的元素绑定到项类的属性。
    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2017-08-03
    • 2019-09-13
    相关资源
    最近更新 更多