我正试图根据我定义的依赖性对象来设计我的图像样式,如下所示:
在MainWindow.xaml:中
<ObjectDataProvider x:Key="GetIsConnected"
ObjectType="{x:Type ConnectionRepository:ConnectionRepository}" />
<Image Name="ConnectStatusBarImage" Width="16" Height="16">
<Image.Style>
<Style>
<Setter Property="Image.Source" Value="/Images/connected16.png" />
<Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource GetIsConnected}, Path=IsConnected}" Value="true">
<Setter Property="Image.Source" Value="/Images/disconnected16.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
存储库代码如下所示:
public class ConnectionRepository : DependencyObject
{
public bool IsConnected
{
get { return (bool)GetValue(IsConnectedProperty); }
set { SetValue(IsConnectedProperty, value); }
}
}
public static readonly DependencyProperty IsConnectedProperty =
DependencyProperty.Register("IsConnected",
typeof(bool), typeof(ConnectionRepository));
如果连接成功,我会将IsConnect设置为true。
但由于某种原因,图像没有根据这个对象而改变。。。知道怎么了吗?
尽管当对象未连接时显示connected16.png
,当连接时切换到disconnected16.png
,但您必须在ObjectDataProvider管理的对象上设置IsConnected
:
ObjectDataProvider p = (ObjectDataProvider)Resources["GetIsConnected"];
((ConnectionRepository)p.Data).IsConnected = true;