2012-09-04 61 views
3

我正在使用mvvm-lightWPF(MVVM)項目,有這樣的問題:我想創建樣式觸發依賴於從我的模型視圖值,但它不工作(MVVM光)style.triggers不工作

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

這似乎是好的,但是當我改變位置沒有HAPP ENS。要檢查我的命令,我將它綁定到TextBox - 所有作品。 哪裏可以解決問題?

+0

你嘗試雙向綁定(我不確定這是否是默認設置)。 '{綁定路徑=位置,模式=雙向}' – McGarnagle

+0

是的,仍然不工作 – ISens

+0

運行時VS中的輸出窗口是否存在綁定錯誤?順便說一句,我已經將您的確切代碼複製到一個空的mvvmlight項目,它工作正常。當我點擊按鈕時,listview的colspan會相應地改變。所以你的代碼的其他部分有些問題... – nemesv

回答

2

您是否嘗試過在綁定上使用UpdateSourceTrigger=PropertyChanged?我有類似的問題,併爲我修復。

<DataTrigger Binding="{Binding Location, UpdateSourceTrigger=PropertyChanged}" Value="0"> 
    <Setter Property="Grid.ColumnSpan" Value="3"></Setter> 
</DataTrigger>