单击DataTemplate中定义的按钮

时间:2011-10-28 12:54:08

标签: wpf

我有一个模板定义如下:

<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>

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

任何人都可以帮助我追踪它吗?

1 个答案:

答案 0 :(得分: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>
相关问题