使用数据绑定更改椭圆填充值
Change Ellipse Fill Value with data binding
我在根据数据绑定布尔值更改 Ellipse
Fill
值时遇到一些问题。
true
= Lime
颜色
false
= Red
颜色
我的代码没有提示任何错误,但也没有显示填充值颜色。
WPF XAML代码:
<Ellipse x:Name="damageSpoolSlot1" HorizontalAlignment="Left" Height="45" Stroke="Black" VerticalAlignment="Top" Width="70" Grid.Column="1" Margin="203.4,377.2,0,0" Grid.Row="2">
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Style.Triggers>
<DataTrigger Binding="{Binding DamageSpoolSlot1PresenceSensorOn}" Value="false">
<Setter Property="Fill" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding DamageSpoolSlot1PresenceSensorOn}" Value="true">
<Setter Property="Fill" Value="Lime"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
代码隐藏:
Binding myBinding = new Binding("DamageSpoolSlot1PresenceSensorOn")
{
Source = MyBinding.Instance
};
BindingOperations.SetBinding(damageSpoolSlot1, Ellipse.FillProperty, myBinding);
我的数据绑定代码:
public class MyBinding : INotifyPropertyChanged
{
private static volatile MyBinding instance;
private static object syncRoot = new Object();
public static MyBinding Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new MyBinding();
}
}
return instance;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private bool _DamageSpoolSlot1PresenceSensorOn = false;
public bool DamageSpoolSlot1PresenceSensorOn
{
get { return _DamageSpoolSlot1PresenceSensorOn; }
set
{
if (value != _DamageSpoolSlot1PresenceSensorOn)
{
_DamageSpoolSlot1PresenceSensorOn = value;
OnPropertyChanged("DamageSpoolSlot1PresenceSensorOn");
}
}
}
}
您样式中的数据触发器绑定到 Ellipse
的 DataContext
。这意味着他们期望一个具有 属性 DamageSpoolSlot1PresenceSensorOn
的对象。但是,您的绑定有两种方式失败。
- 绑定已经绑定到
MyBinding.Instance
的 DamageSpoolSlot1PresenceSensorOn
,因此数据触发器将尝试绑定到另一个根本不存在的子 属性 DamageSpoolSlot1PresenceSensorOn
, DataContext
已经是这个 属性.
- 绑定绑定了
Fill
属性,它期望 Brush
,但数据触发器绑定到 Ellipse
的 DataContext
,而不是Fill
属性.
像这样更改绑定,这样它将 Instace
传送到 DataContext
属性。
Binding myBinding = new Binding()
{
Source = MyBinding.Instance
};
BindingOperations.SetBinding(damageSpoolSlot1, Ellipse.DataContextProperty, myBinding);
另一个更简单的选择是直接在 XAML 中创建到 MyBinding.Instance
的绑定。使用将静态 MyBinding.Instance
指定为带有 x:Static
标记扩展的 Source
的绑定来绑定 DataContext
属性。
<Ellipse x:Name="damageSpoolSlot1" HorizontalAlignment="Left" Height="45" Stroke="Black" VerticalAlignment="Top" Width="70" Margin="203.4,377.2,0,0"
DataContext="{Binding Source={x:Static local:MyBinding.Instance}}">
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Fill" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding DamageSpoolSlot1PresenceSensorOn}" Value="True">
<Setter Property="Fill" Value="Lime"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
正如@Clemens 指出的那样,您也可以简单地在代码隐藏中设置 DataContext
。
damageSpoolSlot1.DataContext = MyBinding.Instance;
我在根据数据绑定布尔值更改 Ellipse
Fill
值时遇到一些问题。
true
=Lime
颜色false
=Red
颜色
我的代码没有提示任何错误,但也没有显示填充值颜色。
WPF XAML代码:
<Ellipse x:Name="damageSpoolSlot1" HorizontalAlignment="Left" Height="45" Stroke="Black" VerticalAlignment="Top" Width="70" Grid.Column="1" Margin="203.4,377.2,0,0" Grid.Row="2">
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Style.Triggers>
<DataTrigger Binding="{Binding DamageSpoolSlot1PresenceSensorOn}" Value="false">
<Setter Property="Fill" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding DamageSpoolSlot1PresenceSensorOn}" Value="true">
<Setter Property="Fill" Value="Lime"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
代码隐藏:
Binding myBinding = new Binding("DamageSpoolSlot1PresenceSensorOn")
{
Source = MyBinding.Instance
};
BindingOperations.SetBinding(damageSpoolSlot1, Ellipse.FillProperty, myBinding);
我的数据绑定代码:
public class MyBinding : INotifyPropertyChanged
{
private static volatile MyBinding instance;
private static object syncRoot = new Object();
public static MyBinding Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new MyBinding();
}
}
return instance;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private bool _DamageSpoolSlot1PresenceSensorOn = false;
public bool DamageSpoolSlot1PresenceSensorOn
{
get { return _DamageSpoolSlot1PresenceSensorOn; }
set
{
if (value != _DamageSpoolSlot1PresenceSensorOn)
{
_DamageSpoolSlot1PresenceSensorOn = value;
OnPropertyChanged("DamageSpoolSlot1PresenceSensorOn");
}
}
}
}
您样式中的数据触发器绑定到 Ellipse
的 DataContext
。这意味着他们期望一个具有 属性 DamageSpoolSlot1PresenceSensorOn
的对象。但是,您的绑定有两种方式失败。
- 绑定已经绑定到
MyBinding.Instance
的DamageSpoolSlot1PresenceSensorOn
,因此数据触发器将尝试绑定到另一个根本不存在的子 属性DamageSpoolSlot1PresenceSensorOn
,DataContext
已经是这个 属性. - 绑定绑定了
Fill
属性,它期望Brush
,但数据触发器绑定到Ellipse
的DataContext
,而不是Fill
属性.
像这样更改绑定,这样它将 Instace
传送到 DataContext
属性。
Binding myBinding = new Binding()
{
Source = MyBinding.Instance
};
BindingOperations.SetBinding(damageSpoolSlot1, Ellipse.DataContextProperty, myBinding);
另一个更简单的选择是直接在 XAML 中创建到 MyBinding.Instance
的绑定。使用将静态 MyBinding.Instance
指定为带有 x:Static
标记扩展的 Source
的绑定来绑定 DataContext
属性。
<Ellipse x:Name="damageSpoolSlot1" HorizontalAlignment="Left" Height="45" Stroke="Black" VerticalAlignment="Top" Width="70" Margin="203.4,377.2,0,0"
DataContext="{Binding Source={x:Static local:MyBinding.Instance}}">
<Ellipse.Style>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Fill" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding DamageSpoolSlot1PresenceSensorOn}" Value="True">
<Setter Property="Fill" Value="Lime"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
正如@Clemens 指出的那样,您也可以简单地在代码隐藏中设置 DataContext
。
damageSpoolSlot1.DataContext = MyBinding.Instance;