2014-01-08 47 views
0

幾個小時後,我放棄了。 我有以下ListView(在網格中)GroupStyle內定義它。 我想以某種方式把它拿出來,並把它放在模板或樣式(我很困惑),然後將它添加到我的ListView的主要風格(ListViewSimpleStyle)。 這樣它可以在其他地方重複使用,而不是每次都複製粘貼。WPF - 將ListView.GroupStyle移動到外部XAML

我該怎麼做?

<ListView Name="LvDataBinding" Grid.Row="0" 
         Style="{StaticResource ListViewSimpleStyle}">            
       <!-- Define the grouping--> 
       <ListView.GroupStyle> 
        <GroupStyle> 
         <GroupStyle.HeaderTemplate> 
          <DataTemplate> 
           <TextBlock FontSize="12" Text="{Binding Name}" 
              Foreground="{StaticResource GrayForgroundBrush}"></TextBlock> 
          </DataTemplate> 
         </GroupStyle.HeaderTemplate> 
        </GroupStyle> 
       </ListView.GroupStyle> 
      </ListView> 

感謝

+0

請看答案這個[問題](http://stackoverflow.com添加分組框中以其風格/ questions/9598859/set-groupstyle-style-on-xaml),它使用附加的行爲。 – sthotakura

回答

0

Groupstyle是GROUPBOX的風格,所以我們需要編輯組框的風格,我已經改變了分組框中HeaderTemplate中,只要你想改變GroupStyle的HeaderTemplate中。

參觀此:http://msdn.microsoft.com/en-us/library/ms754027(v=vs.90).aspx

,並在自己的列表視圖模板樣式即ListViewSimpleStyle

 <Style x:Key="ListViewSimpleStyle" TargetType="ListView"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListView"> 
        <Grid Background="AliceBlue" > 


         <GroupBox> 
          <GroupBox.Style>          
           <Style TargetType="GroupBox"> 
            <Setter Property="HeaderTemplate"> 
             <Setter.Value> 
              <DataTemplate> 
               <TextBlock FontSize="12" Text="{Binding Name}" Foreground="{StaticResource GrayForgroundBrush}"/> 
              </DataTemplate> 
             </Setter.Value> 
            </Setter> 
           </Style> 
          </GroupBox.Style> 
         </GroupBox> 


         <ItemsPresenter></ItemsPresenter> 

        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style>