2011-11-21 78 views
0

我有一個列表框定義爲:如何更改模板列表框中特定元素的樣式?

<ListBox ... ItemTemplate="{DynamicResource User}"/> 

的ItemTemplate是:

<DataTemplate x:Key="User"> 
      <DataTemplate.Resources> 
       <Style TargetType="{x:Type Border}" x:Key="Ordinal"> 
        <Setter Property="Background"> 
         <Setter.Value> 
          ... 
         </Setter.Value> 
        </Setter> 
       </Style> 
       <Style TargetType="{x:Type Border}" x:Key="Current"> 
        <Setter Property="Background"> 
         <Setter.Value> 
          ... 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </DataTemplate.Resources> 
      <Border CornerRadius="5" Padding="5" Margin="0,0,5,0" Style="{StaticResource Ordinal}"> 
       ... 
      </Border> 
     </DataTemplate> 

現在我有一個事件處理程序應當設置項目的風格,提供索引「當前」:

void SetCurrent(int Index) 
{ 
    ? 
} 

如何做到這一點?

編輯:

也許使用的ItemsControl將簡化這個過程?

回答

0

在這種情況下,我會改用組合:

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{StaticResource CurrentBrushKey}" /> 

,並設置列表框的SelectedItem(好 - 不使用的setCurrent()函數,僅僅從邏輯在你的視圖模型)

希望這將有助於

+0

它應該用作DataTemplate的資源? – deha

+0

作爲列表框資源我想 –

相關問題