Question

I'm doing WPF(MVVM) project using mvvm-light libraries, and have such problem: I want to create style trigger dependent on value from my ModelView, but it's do not works

XAML Part :

<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>

Part from 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;
        });
...

It seems to be ok, but when I change Location nothing happens. To check my command, I bind it to TextBox - all works. Where can be problem?

Was it helpful?

Solution

Have you tried using UpdateSourceTrigger=PropertyChanged on your binding? I had a similar problem and that fixed it for me.

<DataTrigger Binding="{Binding Location, UpdateSourceTrigger=PropertyChanged}" Value="0">
     <Setter Property="Grid.ColumnSpan" Value="3"></Setter>
</DataTrigger>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top