2011-11-21 102 views
0

我正在構建一個工具來同時編輯多個數據庫對象,並且很想讓您對高亮方案有所瞭解。
在我的ViewModel中,我有一個Juris,Jurisdictions的集合,這些集合與用戶選擇編輯的任何項目相關聯。我還有一個由所有選定項目「CommonJuris」共享的轄區子集。我的問題是:是否有一種簡單的方法可以用完整集合填充ListBox,並突出顯示子集合中的項目?
目前我有一個布爾屬性,IsCommon,在我的Jurisdiction Model。我真的不想在Model這個,但我還沒有找到更好的解決方案。 Jurisdiction沒有ViewModel,因爲這是唯一的東西。WPF子組突出顯示

這裏是我的列表框本身的XAML:

<ListBox Grid.Row="1" Grid.Column="2" ItemContainerStyle="{StaticResource JuriAndTT}" 
     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     HorizontalContentAlignment="Stretch" 
     ItemsSource="{Binding Juris}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Name}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

而且款式:

<Style TargetType="{x:Type ListBoxItem}" x:Key="JuriAndTT"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding IsCommon}" Value="True"> 
       <Setter Property="Background" Value="PowderBlue"/> 
       <Setter Property="Foreground" Value="Black"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

回答

2

我覺得你這個回答你自己 - 你還沒有創建視圖模型,因爲它會只有一個財產;相反,您已經使用該屬性污染了您的模型。

您必須權衡這兩者,並確定您是否更適合模型中的IsCommon或創建視圖模型的開銷。

還有一些其他方法可以創造性地使用附加的屬性或轉換器,但它們會比實現視圖模型更加複雜和困難。