2012-10-31 67 views
0

我在我的應用程序中有一個expanderview,我試圖在用戶從擴展視圖中選擇一個項目時觸發一個事件,但我不能爲我的生活得到這個工作。在擴展器上的火災事件查看項目選擇

我的XAML看起來像這樣:

<!--Custom header template--> 
<DataTemplate x:Key="CustomHeaderTemplate"> 
    <TextBlock Text="" FontSize="28" />    
</DataTemplate> 

<!--Custom expander template--> 
<DataTemplate x:Key="CustomExpanderTemplate"> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <Rectangle Width="400" Height="60" Fill="#FFF1F1F1" HorizontalAlignment="Stretch" StrokeThickness="0" Grid.Row="0" Grid.Column="0" /> 
     <TextBlock Text="{Binding procedureName}" FontSize="30" Foreground="#FF00457C" FontWeight="Normal" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="10,0,0,0" /> 

    </Grid> 

</DataTemplate> 

<!--Custom expander items template--> 
<DataTemplate x:Key="ExpanderViewItems" > 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="15" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <Image Source="{Binding flagIcon}" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" /> 
     <TextBlock FontSize="26" Text="{Binding N}" Foreground="Black" FontWeight="Normal" Grid.Row="0" Grid.Column="1"/> 
     <TextBlock FontSize="20" Text="{Binding RNG}" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"/>     
     <TextBlock FontSize="26" Text="{Binding ValueAndUnit}" Foreground="Black" FontWeight="Medium" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/> 
     <TextBlock FontSize="18" Text="{Binding COM}" Foreground="Black" FontWeight="Normal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" TextWrapping="Wrap" /> 
     <Line StrokeThickness="1" Stroke="#C4C6CC" Stretch="Fill" X1="0" X2="1" Y1="0" Y2="0" VerticalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" /> 
    </Grid> 
</DataTemplate> 

<!--Listbox Containing ExpanderViews--> 
     <ListBox Name="testsList" Grid.Row="3" Grid.Column="0" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 

        <!--ExpanderView--> 
        <toolkit:ExpanderView Header="{Binding}"             
              HeaderTemplate="{StaticResource CustomHeaderTemplate}" 
              Expander="{Binding}" 
              ExpanderTemplate="{StaticResource CustomExpanderTemplate}" 
              x:Name="expander" 
              FontSize="36" 
              Foreground="#FF00457C" 
              ItemsSource="{Binding testItems}" 
              ItemTemplate="{StaticResource ExpanderViewItems}" >       
        </toolkit:ExpanderView> 

       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

所以你有它,只需要在用戶選擇的擴展項目簡單地觸發一個事件,發現它非常乏味。

感謝您的任何建議!

回答

0

您應該使用MouseLeftButtonUp事件處理程序在你的ItemTemplate的項容器是一個Grid控件:

<!--Custom expander items template--> 
<DataTemplate x:Key="ExpanderViewItems" > 

    <Grid MouseLeftButtonUp="ItemSelected"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="15" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <Image Source="{Binding flagIcon}" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" /> 
     <TextBlock FontSize="26" Text="{Binding N}" Foreground="Black" FontWeight="Normal" Grid.Row="0" Grid.Column="1"/> 
     <TextBlock FontSize="20" Text="{Binding RNG}" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"/>     
     <TextBlock FontSize="26" Text="{Binding ValueAndUnit}" Foreground="Black" FontWeight="Medium" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/> 
     <TextBlock FontSize="18" Text="{Binding COM}" Foreground="Black" FontWeight="Normal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" TextWrapping="Wrap" /> 
     <Line StrokeThickness="1" Stroke="#C4C6CC" Stretch="Fill" X1="0" X2="1" Y1="0" Y2="0" VerticalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" /> 
    </Grid> 
</DataTemplate> 
+0

感謝您的答覆!我嘗試過,但不是在選擇expanderViewItem時觸發事件,而是在點擊expanderViewExpander查看其所有項目後觸發事件... – Tiwaz89

+0

因此,在單擊下拉框時觸發它,而不是在下拉框中的項目是點擊。 – Tiwaz89

+0

我編輯了我的答案,告訴我現在是否可以。 –