我正在使用WPF(MVVM)
库做mvvm-light
项目,并且遇到了这样的问题:我想创建依赖于我的ModelView值的样式触发器,但它不起作用
XAML
部分:
<Window...
<Window.DataContext>
<!-- Declaratively create an instance of our Alarm View Model-->
<local:AlarmView />
</Window.DataContext>
....
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Command="{Binding ChangeLocation}" Content="Click"></Button>
<ListView
Grid.Row="1"
Name="LV1"
ItemsSource="{Binding Items}"
>
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Style.Triggers>
<DataTrigger Binding="{Binding Location}" Value="0">
<Setter Property="Grid.ColumnSpan" Value="3"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Location}" Value="1">
<Setter Property="Grid.ColumnSpan" Value="1"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Location}" Value="2">
<Setter Property="Grid.ColumnSpan" Value="2"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Location}" Value="3">
<Setter Property="Grid.ColumnSpan" Value="3"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
</Grid>
</Window>
部分来自ModelView
:
class AlarmView : ViewModelBase
{
...
int _location = 0;
...
public int Location
{
get { return _location; }
set
{
_location = value;
RaisePropertyChanged("Location");
}
}
...
public RelayCommand ChangeLocation { get; set; }
...
ChangeLocation = new RelayCommand(() =>
{
if (Location < 3)
{
Location++;
}
else Location = 0;
});
...
似乎没问题,但是当我改变位置时没有任何反应。要检查我的命令,我将其绑定到TextBox
- 一切正常。
哪里有问题?
答案 0 :(得分:2)
您是否尝试在绑定中使用UpdateSourceTrigger=PropertyChanged
?我有一个类似的问题,并为我修复它。
<DataTrigger Binding="{Binding Location, UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="Grid.ColumnSpan" Value="3"></Setter>
</DataTrigger>