2010-06-29 128 views
1
<Style x:Key="OrderGroupTemplateStyle" TargetType="{x:Type ContentControl}"> 
<Style.Triggers> 
    <DataTrigger Binding="{Binding Path=Name.ShowDetailedInfo, UpdateSourceTrigger=PropertyChanged}" Value="False"> 
     <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="3" Margin="2"> 
        <StackPanel Background="LightGoldenrodYellow"> 
        <ContentControl Content="{Binding Path=.}" Style="{StaticResource MyRecordViewModelShortStyle}"/> 
        <ListView ItemsSource="{Binding Path=Items}" Margin="4">                  
        <ListView.ItemContainerStyle> 
         <Style TargetType="{x:Type ListViewItem}"> 
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />       <Setter Property="Padding" Value="2"/> 
          <EventSetter Event="MouseDoubleClick" Handler="ItemsControl_SelectionChanged"/> 
                 </Style> 
                </ListView.ItemContainerStyle> 

我想在listview選擇更改時做一些工作。因爲我正在使用樣式,所以我無法在ListView上使用SelectionChanged事件。我試圖用EventSetter但在編譯項目中的任何錯誤:不能指定在一個樣式一個目標標籤 風格內的WPF ListView SelectionChanged不起作用。 EventSetter

事件「MouseDoubleClick」。 改爲使用EventSetter。

有人能幫我嗎?

回答

4

嘗試將Style創建爲資源而不是將其聲明爲內聯。我不知道爲什麼它的行爲不同,但它似乎使錯誤消失:

<Style TargetType="{x:Type ListViewItem}" x:Key="ItemContainerStyle"> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="Padding" Value="2"/> 
    <EventSetter Event="MouseDoubleClick" Handler="ItemsControl_SelectionChanged"/> 
</Style> 
<Style x:Key="OrderGroupTemplateStyle" TargetType="{x:Type ContentControl}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Path=Name.ShowDetailedInfo, UpdateSourceTrigger=PropertyChanged}" Value="False"> 
      <Setter Property="ContentTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="3" Margin="2"> 
          <StackPanel Background="LightGoldenrodYellow"> 
           <ContentControl Content="{Binding Path=.}" Style="{StaticResource MyRecordViewModelShortStyle}"/> 
           <ListView ItemsSource="{Binding Path=Items}" Margin="4" ItemContainerStyle="{StaticResource ItemContainerStyle}"/> 
1

我不明白的語句「因爲我使用的風格,我不能在ListView中使用SelectionChanged事件」

但是你可以使用的ListView SelectionChanged事件,如果使用的是樣式也。

+1

其實我不能。當我嘗試使用它時出現以下錯誤:錯誤無法在樣式中的Target標籤上指定事件'SelectionChanged'。改用EventSetter。 – 2010-06-29 10:47:18

+0

嘗試此 <形式X:鍵= 「OrderGroupTemplateStyle」 的TargetType = 「{x:類型ContentControl中}」> 。 .. – Ragunathan 2010-06-30 05:00:27