2011-10-28 57 views
1

定义的按钮,我有如下模板defiend:点击里面的DataTemplate

<Window.Resources> 
    <DataTemplate x:Key="TemplateDetailView"> 
      <Expander x:Name="exp" IsExpanded="True" FontSize="13" FontWeight="SemiBold"> 
       <Expander.Header> 
        <TextBlock Foreground="{DynamicResource BlackColorBrush}"> 
         <TextBlock.Text> 
          <MultiBinding StringFormat="{} {0} {1}"> 
           <Binding Path="persons.Count"/> 
           <Binding Path="DisplayText"/> 
          </MultiBinding> 
         </TextBlock.Text> 
        </TextBlock> 
       </Expander.Header> 

       <ListBox x:Name="lst" ItemsSource="{Binding persons}" Grid.Row="1" BorderThickness="0" Foreground="{DynamicResource BlackColorBrush}"> 

        <ListBox.ItemContainerStyle> 
         <Style TargetType="{x:Type ListBoxItem}"> 
          <Setter Property="HorizontalContentAlignment" Value="stretch"/> 
          <Setter Property="Background" Value="Transparent" /> 
         </Style> 
        </ListBox.ItemContainerStyle> 

        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <Border DataContext="{Binding}" BorderThickness="2" Margin="0,0,0,-1" BorderBrush="{DynamicResource NormalBorderBrush}" Visibility="{Binding IsVisibility}"> 
           <DockPanel Margin="15,5" Background="Transparent"> 
            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Background="Transparent"> 
             <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> 
              <CheckBox VerticalAlignment="Center" VerticalContentAlignment="Center" Tag="{Binding}" Padding="4,0" Style="{DynamicResource CheckBoxStyleDetailedViewStyle}" Checked="CheckBox_Checked" IsChecked="{Binding Acknowledged}" Height="30" Margin="3,5,3,3" Width="Auto"/> 
             </StackPanel>                    <ScrollViewer CanContentScroll="True" MinHeight="25" DockPanel.Dock="Left" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled"> 
              <ItemsControl ScrollViewer.CanContentScroll="True" DataContext="{Binding}" ItemsSource="{Binding AlertActionsDefinition.Children}" x:Name="lstButton" ButtonBase.Click="lstButton_Click"> 

               <ItemsControl.ItemsPanel> 
                <ItemsPanelTemplate> 
                 <StackPanel Orientation="Horizontal" Background="Transparent" > 
                 </StackPanel> 
                </ItemsPanelTemplate> 
               </ItemsControl.ItemsPanel> 
               <ItemsControl.ItemTemplate > 
                <DataTemplate> 
                 <Button Click="lstButton_Click" Content="{Binding Text}" Tag="{Binding}" Padding="4,0" IsEnabled="{Binding Visible}" Visibility="{Binding Visibility}" Height="30" Margin="3,5,3,3"/> 
                              </DataTemplate> 
               </ItemsControl.ItemTemplate> 
              </ItemsControl> 
             </ScrollViewer>         </StackPanel> 

            <Grid Width="700"> 
             <Grid.RowDefinitions> 
              <RowDefinition Height="3"/> 
              <RowDefinition Height="Auto"/> 
              <RowDefinition Height="3"/> 
              <RowDefinition Height="Auto"/> 
              <RowDefinition Height="3"/> 
              <RowDefinition Height="Auto"/> 
              <RowDefinition Height="3"/> 
              <RowDefinition Height="*"/> 
              <RowDefinition Height="3"/> 
             </Grid.RowDefinitions> 
             <TextBlock Text="{Binding Text}" Grid.Row="1" FontWeight="Bold" FontSize="14"/> 
             <TextBlock Text="{Binding Description}" Grid.Row="3" FontSize="13"/> 
                         </Grid> 

           </DockPanel> 
          </Border> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 

       </ListBox> 
      </Expander> 
      <DataTemplate.Triggers> 
       <DataTrigger Binding="{Binding ElementName=lst,Path=Items.Count}" Value="0"> 
        <Setter Property="Visibility" Value="Collapsed" TargetName="exp"/> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 
</Window.Resources> 
<Grid> 
    <ListBox x:Name="listBox" ItemTemplate="{DynamicResource TemplateShortView}" ItemsSource="{Binding}" BorderThickness="0"> 
</ListBox>      
</Grid> 

列表框被绑定到一类人。最初列表框中加载了短视图,然后加载到上面定义的详细视图模板。我面临的问题是模板内按钮上的点击事件。点击不会被解雇,有时候需要两到三次点击来提高事件。

任何人都可以帮我找出来吗?

回答

0

我减少了你的代码以适合我的测试,它对我来说非常适合!我正确接收所有事件。事实上,我收到每个按钮点击两次的lstButton_Click ...(由于冒泡ButtonItemsControl级别)。

代码隐藏...

/// <summary> 
/// Interaction logic for Window6.xaml 
/// </summary> 
public partial class Window6 : Window 
{ 
    public Window[] JustList 
    { 
     get 
     { 
      return new Window[] { this }; 
     } 
    } 

    public List<Person> persons 
    { 
     get 
     { 
      return new List<Person>() 
         { 
          new Person() 
           { 
            Acknowledged = true, 
            Description = "Person 1", 
            Text = "Person1", 
            DisplayText = "I am Person 1" 
           }, 
          new Person() 
           { 
            Acknowledged = true, 
            Description = "Person 2", 
            Text = "Person2", 
            DisplayText = "I am Person 2" 
           } 
         }; 
     } 
    } 

    public Window6() 
    { 
     InitializeComponent(); 
    } 

    void lstButton_Click(object sender, RoutedEventArgs e) 
    { 
     var i = 0 ; 
    } 

    void CheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     var i = 0; 
    } 
} 

public class Person 
{ 
    public string DisplayText { get; set; } 
    public string Text { get; set; } 
    public bool Acknowledged { get; set; } 
    public string Description { get; set; } 
    public Visibility IsVisibility { get; set; } 

    public List<Person> Children 
    { 
     get 
     { 
      return new List<Person>() 
         { 
          new Person() 
           { 
            Acknowledged = true, 
            Description = "Child 1", 
            Text = "Child1", 
            DisplayText = "My Child 1" 
           }, 
          new Person() 
           { 
            Acknowledged = true, 
            Description = "Child 2", 
            Text = "Child2", 
            DisplayText = "My Child 2" 
           } 
         }; 
     } 
    } 
} 

XAML ...

<Window.Resources> 
    <DataTemplate x:Key="TemplateDetailView"> 
     <Expander x:Name="exp" IsExpanded="True" 
        FontSize="13" FontWeight="SemiBold"> 
      <Expander.Header> 
       <TextBlock> 
        <TextBlock.Text> 
         <MultiBinding StringFormat="{} {0} {1}"> 
         <Binding Path="persons.Count"/> 
         <Binding Path="DisplayText"/> 
         </MultiBinding> 
        </TextBlock.Text> 
       </TextBlock> 
      </Expander.Header> 

      <ListBox x:Name="lst" ItemsSource="{Binding persons}" 
        Grid.Row="1" BorderThickness="0"> 

       <ListBox.ItemContainerStyle> 
        <Style TargetType="{x:Type ListBoxItem}"> 
         <Setter Property="HorizontalContentAlignment" 
           Value="stretch"/> 
         <Setter Property="Background" Value="Transparent" /> 
        </Style> 
       </ListBox.ItemContainerStyle> 

       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Border DataContext="{Binding}" 
           BorderThickness="2" 
           Margin="0,0,0,-1" BorderBrush="Red"> 
          <DockPanel Margin="15,5" 
             Background="Transparent"> 
           <StackPanel DockPanel.Dock="Bottom" 
              Orientation="Horizontal" 
              Background="Transparent"> 
            <StackPanel HorizontalAlignment="Left" 
               Orientation="Horizontal"> 
             <CheckBox VerticalAlignment="Center" 
                VerticalContentAlignment="Center" 
                Tag="{Binding}" 
                Padding="4,0" 
                Checked="CheckBox_Checked" 
                IsChecked="{Binding Acknowledged}" 
                Height="30" 
                Margin="3,5,3,3" Width="Auto"/> 
            </StackPanel> 
            <ScrollViewer CanContentScroll="True" 
                MinHeight="25" 
                DockPanel.Dock="Left" 
                HorizontalScrollBarVisibility="Auto" 
               VerticalScrollBarVisibility="Disabled"> 
             <ItemsControl 
              ScrollViewer.CanContentScroll="True" 
              DataContext="{Binding}" 
              ItemsSource="{Binding Children}" 
              x:Name="lstButton" 
              ButtonBase.Click="lstButton_Click"> 

              <ItemsControl.ItemsPanel> 
               <ItemsPanelTemplate> 
                <StackPanel 
                 Orientation="Horizontal" 
                 Background="Transparent" > 
                </StackPanel> 
               </ItemsPanelTemplate> 
              </ItemsControl.ItemsPanel> 
              <ItemsControl.ItemTemplate > 
               <DataTemplate> 
                <Button Click="lstButton_Click" 
                  Content="{Binding Text}" 
                  Tag="{Binding}" 
                  Padding="4,0" 
                  IsEnabled="{Binding 
                   Visible}" 
                  Visibility="{Binding 
                   Visibility}" 
                  Height="30" 
                  Margin="3,5,3,3"/> 
               </DataTemplate> 
              </ItemsControl.ItemTemplate> 
             </ItemsControl> 
            </ScrollViewer> 
           </StackPanel> 

           <Grid Width="700"> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="3"/> 
             <RowDefinition Height="Auto"/> 
             <RowDefinition Height="3"/> 
             <RowDefinition Height="Auto"/> 
             <RowDefinition Height="3"/> 
             <RowDefinition Height="Auto"/> 
             <RowDefinition Height="3"/> 
             <RowDefinition Height="*"/> 
             <RowDefinition Height="3"/> 
            </Grid.RowDefinitions> 
            <TextBlock Text="{Binding Text}" Grid.Row="1" 
               FontWeight="Bold" FontSize="14"/> 
             <TextBlock Text="{Binding Description}" 
                Grid.Row="3" FontSize="13"/> 
            </Grid> 
          </DockPanel> 
         </Border> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </Expander> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding ElementName=lst,Path=Items.Count}" 
         Value="0"> 
       <Setter Property="Visibility" Value="Collapsed" TargetName="exp"/> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 
</Window.Resources> 

<Grid> 
    <ListBox x:Name="listBox" ItemTemplate="{StaticResource TemplateDetailView}" 
      ItemsSource="{Binding RelativeSource={RelativeSource 
             AncestorType={x:Type Window}}, 
            Path=JustList}" 
      BorderThickness="0"> 
    </ListBox> 
</Grid>