c# - 根据 bool 值更改椭圆的颜色

标签 c# wpf datatrigger

<Grid Grid.Row="1" Width="500" Height="500">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Row="1"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Row="3"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Column="4"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Column="4" Grid.Row="4"/>
</Grid>

鉴于上述 XAML,当属性为真时,我希望点为绿色。我假设我会用 DataTrigger 来完成它,但我能想到的唯一方法是为每个椭圆复制它。这对我来说似乎很老套,并且想知道他们是否是更好的解决方案。每个椭圆都基于一个属性,但这又像是很多重复代码。理想情况下,我想要的是此 View 使用 bool 值来反射(reflect)“站点”列表的状态,以确定它们是否可用。每一个的状态都是单向的,并且在 View 打开时不会改变。

我对 WPF 和 XAML 还很陌生,无法提出一个优雅的解决方案。每次我尝试做某事时我都会畏缩,因为它看起来像是一个完全的 hack。

编辑: 感谢@Alastair 的回答,我已经开始工作了。

最佳答案

所以我会定制一个UserControl包含一个椭圆。

然后您可以将数据触发器放入 UserControl .然后绑定(bind) DataContext自定义控件到您的 bool 属性,然后绑定(bind) DataTriggerDataContext你的UserControl .

因此,您可以保持 XAML 干净。

编辑:

一个基本的用户控件。这应该在一个单独的文件中定义,而不是资源。只需右键单击项目 -> 添加 -> 新项...然后选择 WPF UserControl。

<UserControl x:Class="Test_WPF.MyEllipseControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Ellipse  HorizontalAlignment="Center"
                  Height="25"
                  Margin="0,0,0,0"
                  Stroke="Black"
                  VerticalAlignment="Center"
                  Width="25" 
                  Fill="Red">
            <Ellipse.Style>
                <Style TargetType="Ellipse">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsAvailable}"
                                     Value="True">
                            <Setter Property="Fill"
                                    Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Ellipse.Style>
        </Ellipse>
    </Grid>
</UserControl>

然后你会使用它:

<local:MyEllipseControl DataContext="{Binding Path=Station1}" />

哪里local命名空间只是您的本地项目命名空间。

关于c# - 根据 bool 值更改椭圆的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16138985/

相关文章:

c# - 在 telerik bing map 提供商中设置默认位置

WPF 数据触发器

WPF动画 "Cannot freeze this Storyboard timeline tree for use across threads"

c# - 在 Excel 中查找图像位置

c# - OWIN Startup 方法如何获取网站的基本 URL?

wpf - TextBlock 文本绑定(bind) ObservableCollection.Count 属性

c# - 使用 setwindowshookex (user32.dll) 禁用触摸屏鼠标设备事件

c# - 系统.线程.任务.并行

c# - 创建文本文件,将它们压缩到 ZipArchive 中并提供以供下载

c# - WPF 触发器/绑定(bind)不起作用