【发布时间】: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 的椭圆元素并更改其样式(例如,它的颜色)。
这可能吗?
编辑
从您的回答中,我知道如果复选框和椭圆都属于同一个模板,我可以做到这一点。我发现的问题(对不起,我忘了提)是目前我正在使用一个网格,其中一行包含省略号,另一行包含在运行时创建的复选框(这就是我使用的原因代码隐藏):
【问题讨论】: