wpf 根据布尔值更改椭圆的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16138985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of an ellipse based off a boolean
提问by lose_the_grimm
<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>
Given the above XAML I want the dots to be green when a property is true. I'm assuming I'd do it with a DataTrigger, but the only way I can think of doing it involved duplicating it for each ellipse. It seems hackish to me and was wondering if their is a better solution. Each ellipse is based of a single property, but again that seems like a lot of duplicate code. Ideally what I want is this view to reflect the state of a list of "Stations" using a boolean to determine if they're available or not. The state of each is one-way and won't change while the view is up.
鉴于上述 XAML,当属性为真时,我希望点为绿色。我假设我会用 DataTrigger 来做,但我能想到的唯一方法是为每个椭圆复制它。这对我来说似乎很hackish,并且想知道他们是否是更好的解决方案。每个椭圆都基于一个属性,但这似乎又是很多重复的代码。理想情况下,我想要的是这个视图使用布尔值来反映“站”列表的状态,以确定它们是否可用。每个的状态都是单向的,并且在视图打开时不会改变。
I'm far to new at WPF and XAML to come up with an elegant solution. I cringe every time I attempt something as it just seems like a complete hack.
我对 WPF 和 XAML 还很陌生,无法提出一个优雅的解决方案。每次我尝试一些东西时我都会畏缩,因为它看起来像是一个完整的黑客。
EDIT: Thanks to @Alastair's answer I've gotten it working.
编辑:感谢@Alastair 的回答,我已经开始工作了。
回答by Alastair Pitts
So I would make a custom UserControlthat contains an Ellipse.
所以我会做一个UserControl包含椭圆的自定义。
You can then put a the datatrigger into the UserControl. You then bind the DataContextof the custom control to your boolean property, then bind the DataTriggerto the DataContextof your UserControl.
然后,您可以将数据触发器放入UserControl. 然后,您可以绑定DataContext自定义控件到您的布尔属性,然后在绑定DataTrigger到DataContext你的UserControl。
Thus you are able to keep your XAML clean.
因此,您可以保持 XAML 干净。
EDIT:
编辑:
A basic user control. THis should be defined in a separate file, as opposed to a resource. Just right-click the project -> Add -> New Item... then choose WPF UserControl.
一个基本的用户控件。这应该在一个单独的文件中定义,而不是一个资源。只需右键单击项目 -> 添加 -> 新项目...然后选择 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>
and then you'd use it:
然后你会使用它:
<local:MyEllipseControl DataContext="{Binding Path=Station1}" />
where the localnamespace is just your local projects namespace.
其中local命名空间只是您的本地项目命名空间。
回答by Brandorf
Another approach is to bind the fill to your Boolean value directly and use a converter to change the style as you want.
另一种方法是直接将填充绑定到您的布尔值,并使用转换器根据需要更改样式。
Your converter might look something like this:
您的转换器可能如下所示:
class StatusToColor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
SolidColorBrush retColor = new SolidColorBrush();
retColor.Color = System.Windows.Media.Color.FromRgb(0, 0, 0);
if ((bool)value)
{
retColor.Color = System.Windows.Media.Color.FromRgb(255, 0, 0);
}
else
{
retColor.Color = System.Windows.Media.Color.FromRgb(0, 128, 0);
}
return retColor;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
You can then define the converter in your xaml resources:
然后您可以在您的 xaml 资源中定义转换器:
<Window>
<Window.Resources>
<c:StatusToColor x:Key="MyConverter"/>
</Window.Resources>
...
And set up your bindings as such:
并设置您的绑定:
<Ellipse Fill="{Binding SomeProperty, Converter={StaticResource MyConverter}}" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25"/>

