我在从依赖项属性设置Image时遇到问题。似乎触发器没有触发。我只想隐藏/显示和图像,或者尽可能设置源。
public static readonly DependencyProperty HasSingleValueProperty =
DependencyProperty.Register("HasSingleValue", typeof(bool), typeof(LevelControl), new
FrameworkPropertyMetadata(false,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public bool HasSingleValue
{
get { return (bool)GetValue(HasSingleValueProperty); }
set { SetValue(HasSingleValueProperty, value); }
}
public LevelControl()
{
this.InitializeComponent();
//this.DataContext = this;
LayoutRoot.DataContext = this;
}
//Control Markup
<Grid x:Name="LayoutRoot">
<Image x:Name="xGreenBarClientTX" HorizontalAlignment="Stretch" Height="13" Margin="7,8.5,7,0"
Stretch="Fill"
VerticalAlignment="Top"
Width="47"
Canvas.Left="181.67"
d:LayoutOverrides="Height" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding HasSingleValue}" Value="True">
<Setter Property="Opacity" Value="100"/>
</DataTrigger>
<DataTrigger Binding="{Binding HasSingleValue}" Value="False">
<Setter Property="Opacity" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
答案 0 :(得分:0)
我认为问题可能在于您是如何尝试更新依赖项属性的,因为我对您的示例进行了轻微更改以验证绑定,这样可以按预期工作。我曾怀疑从图像绑定到网格数据上下文中的属性可能存在问题,但这似乎没问题。
目前尚不清楚您作为数据上下文提供了哪种类,但如果您使用依赖项属性,那么它需要是DependencyObject。
我在这里提供了一个示例,使用文本块和切换按钮来更改依赖项属性。 (假设您正在使用代码隐藏)
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="xGreenBarClientTX" HorizontalAlignment="Stretch" Height="13" Margin="7,8.5,7,0"
VerticalAlignment="Top"
Width="200"
Canvas.Left="181.67" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding HasSingleValue}" Value="True">
<Setter Property="Text" Value="Is Set"/>
</DataTrigger>
<DataTrigger Binding="{Binding HasSingleValue}" Value="False">
<Setter Property="Text" Value="Is Not Set"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<Button Grid.Column="1" Content="Toggle" Click="Button_Click" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
和代码隐藏:
public MainWindow()
{
InitializeComponent();
LayoutRoot.DataContext = new VM() { HasSingleValue = true };
}
private void Button_Click( object sender, RoutedEventArgs e )
{
var vm = LayoutRoot.DataContext as VM;
if ( vm == null )
return;
vm.HasSingleValue = !vm.HasSingleValue;
}
public class VM : DependencyObject
{
public static readonly DependencyProperty HasSingleValueProperty =
DependencyProperty.Register( "HasSingleValue", typeof( bool ), typeof( VM ), new FrameworkPropertyMetadata( false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault ) );
public bool HasSingleValue
{
get { return (bool) GetValue( HasSingleValueProperty ); }
set { SetValue( HasSingleValueProperty, value ); }
}
}
答案 1 :(得分:0)
我们走了 你必须按如下方式更新你的绑定(我使用Ellipse代替测试)
<Window
x:Name="myWindow">
<Style TargetType="{x:Type Ellipse}">
<Style.Triggers>
<DataTrigger Binding="{Binding HasSingleValue, ElementName=myWindow}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding HasSingleValue, ElementName=myWindow}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
然后你需要设置HasSingleValue 我创建了两个按钮来显示和隐藏我的椭圆。