我正在尝试根据我定义的依赖对象来设置我的图像样式:
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。
但由于某种原因,图像没有根据这个对象改变......并且想法出了什么问题?
答案 0 :(得分:0)
尽管您在对象未连接时显示connected16.png
,并且当 连接时切换到disconnected16.png
,必须在ObjectDataProvider管理的对象上设置IsConnected
:
ObjectDataProvider p = (ObjectDataProvider)Resources["GetIsConnected"];
((ConnectionRepository)p.Data).IsConnected = true;