我正在尝试根据我定义如下的依赖对象来设置我的图像样式:
在 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。
但由于某种原因,图像并没有根据这个对象发生变化......并且知道有什么问题吗?