3

我正在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- all works。哪里可能有问题?

4

1 回答 1

2

你试过UpdateSourceTrigger=PropertyChanged在你的绑定上使用吗?我有一个类似的问题,并为我解决了这个问题。

<DataTrigger Binding="{Binding Location, UpdateSourceTrigger=PropertyChanged}" Value="0">
     <Setter Property="Grid.ColumnSpan" Value="3"></Setter>
</DataTrigger>
于 2012-09-07T07:46:20.597 回答