Divyashree
Divyashree

Reputation: 11

click of a button defined inside DataTemplate

i have a template defiend as below:

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

listbox is binded to class persons. initially the list box is loaded with short view which is then loaded to have detailed view template defined above. the problem i am facing is the click event on button inside template. click wont be fired at all some times and some times takes two to three clicks to raise the event.

could any one help me out to trace it down ?

Upvotes: 1

Views: 2984

Answers (1)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

I reduced your code to suit my testing and it works perfectly for me! I receive all the events correctly. In fact I receive lstButton_Click twice for each button clicked... (due to bubbling at Button and ItemsControl level).

Code Behind...

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

Upvotes: 1

Related Questions