los*_*imm 1 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>
Run Code Online (Sandbox Code Playgroud)
鉴于上述XAML,我希望当属性为true时,点为绿色.我假设我是用DataTrigger做的,但我能想到的唯一方法就是为每个椭圆重复它.这对我来说似乎是个hackish,并想知道他们是否是一个更好的解决方案.每个椭圆都基于一个属性,但同样看起来像是很多重复的代码.理想情况下,我想要的是这个视图使用布尔值来反映"站点"列表的状态,以确定它们是否可用.每个状态都是单向的,并且在视图启动时不会改变.
我很擅长WPF和XAML,想出一个优雅的解决方案.每当我尝试一些东西时我都会畏缩,因为它看起来像是一个完整的黑客.
编辑:感谢@ Alastair的回答我得到了它的工作.
所以我会制作一个UserControl包含Ellipse 的自定义.
然后,您可以将数据触发器放入UserControl.然后DataContext,将自定义控件的绑定绑定到布尔属性,然后绑定DataTrigger到DataContext您的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>
Run Code Online (Sandbox Code Playgroud)
然后你会用它:
<local:MyEllipseControl DataContext="{Binding Path=Station1}" />
Run Code Online (Sandbox Code Playgroud)
其中,local命名空间就是你的地方项目的命名空间.
另一种方法是将填充直接绑定到布尔值,然后使用转换器根据需要更改样式。
您的转换器可能看起来像这样:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在xaml资源中定义转换器:
<Window>
<Window.Resources>
<c:StatusToColor x:Key="MyConverter"/>
</Window.Resources>
...
Run Code Online (Sandbox Code Playgroud)
并这样设置绑定:
<Ellipse Fill="{Binding SomeProperty, Converter={StaticResource MyConverter}}" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7042 次 |
| 最近记录: |