如果设置了VM中的标志,我想扩展器扩展。我还希望用户能够覆盖它并随意扩展/折叠。以下代码不起作用,计时器启动并且扩展器重复扩展和折叠 - 然后如果您手动单击扩展器,它也会切换 - 但触发器无法展开或折叠扩展器。它当然好像设置了手动键控值并且优先于触发器设置器。
<Expander Header="Test" BorderThickness="2" BorderBrush="Black" VerticalAlignment="Bottom">
<Expander.Style >
<Style TargetType="Expander">
<Setter Property="IsExpanded" Value="True"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.AmSet,
RelativeSource={RelativeSource AncestorType=Grid}}"
Value="True">
<Setter Property="IsExpanded" Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Content>
<Border Background="AliceBlue" Width="50" Height="50"></Border>
</Expander.Content>
VM有一个虚拟计时器,它只是切换标志以触发更新,如下所示
public class vm : INotifyPropertyChanged
{
public vm()
{
t = new System.Timers.Timer(1000);
t.Elapsed += t_Elapsed;
t.Start();
}
bool _AmSet = false;
public bool AmSet
{
get { return _AmSet; }
set
{
_AmSet = value;
OnPropertyChanged("");
}
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
AmSet = !AmSet;
}
System.Timers.Timer t;
private void OnPropertyChanged(string prop)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
public event PropertyChangedEventHandler PropertyChanged;
}
答案 0 :(得分:0)
是否有理由需要使用DataTrigger执行此操作?通过双向绑定可以轻松实现。
<Expander Header="Test" BorderThickness="2" BorderBrush="Black" VerticalAlignment="Bottom" IsExpanded="{Binding AmSet, Mode=TwoWay}"/>