WPF Custom ListBox as Buttons Click not firing

Posted by Ryan on Stack Overflow See other posts from Stack Overflow or by Ryan
Published on 2010-04-23T16:21:29Z Indexed on 2010/04/23 16:23 UTC
Read the original article Hit count: 363

Filed under:
|
|
|

I am attempting to have a ListBox of TextBoxes with click events. I have read that one way to achieve this was to have a list of Buttons and call ButtonBase.Click="" on the ListBox. This was not working. Any advice as to how I would hook up a click event to the listbox items?

Thanks

  <Window.Resources>

    <ControlTemplate x:Key="MouseOverFocusTemplate" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="55*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                         Foreground="#FF6FB8FD"
                                         FontSize="24"
                                         TextWrapping="Wrap"
                                         Text="{Binding .}"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         MinHeight="55"
                                         Cursor="Hand"
                                         IsReadOnly="True"
                                         FontFamily="Arial"
                                         >
                <TextBox.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF013B73" Offset="0.501"/>
                        <GradientStop Color="#FF091F34"/>
                        <GradientStop Color="#FF014A8F" Offset="0.5"/>
                        <GradientStop Color="#FF003363" Offset="1"/>
                    </LinearGradientBrush>
                </TextBox.Background>

            </TextBox>
        </Grid>
    </ControlTemplate>
    <Style x:Key="MouseOverFocusStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Template" Value="{StaticResource MouseOverFocusTemplate}"/>
    </Style>

    <ControlTemplate x:Key="LostFocusTemplate" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="55*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                         Foreground="#FF6FB8FD"
                                         FontSize="24"
                                         TextWrapping="Wrap"
                                         Text="{Binding .}"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         MinHeight="55"
                                         Cursor="Hand"
                                         IsReadOnly="True"
                                         FontFamily="Arial"
                                         >
                <TextBox.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <LinearGradientBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform CenterX="0.5" CenterY="0.5"/>
                                <SkewTransform CenterX="0.5" CenterY="0.5"/>
                                <RotateTransform CenterX="0.5" CenterY="0.5"/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </LinearGradientBrush.RelativeTransform>
                        <GradientStop Color="#FF091F34" Offset="1"/>
                        <GradientStop Color="#FF002F5C" Offset="0.4"/>
                    </LinearGradientBrush>
                </TextBox.Background>

            </TextBox>
        </Grid>
    </ControlTemplate>
    <Style x:Key="LostFocusStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Template" Value="{StaticResource LostFocusTemplate}"/>
    </Style>

    <ControlTemplate x:Key="GotFocusTemplate" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="55*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
                <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                         Foreground="#FFE38E27"
                                         FontSize="24"
                                         TextWrapping="Wrap"
                                         Text="{Binding .}"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         MinHeight="55"
                                         Cursor="Hand"
                                         IsReadOnly="True"
                                         FontFamily="Arial"
                                         >
                    <TextBox.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0.501"/>
                        <GradientStop Color="#FF091F34"/>
                        <GradientStop Color="#FF002F5C" Offset="0.5"/>
                    </LinearGradientBrush>
                </TextBox.Background>

                </TextBox>
        </Grid>
    </ControlTemplate>
    <Style x:Key="GotFocusStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Template" Value="{StaticResource GotFocusTemplate}"/>
    </Style>

    <Style TargetType="{x:Type Button}" x:Key="listButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="55*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                                    <SkewTransform AngleX="0" AngleY="0"/>
                                    <RotateTransform Angle="0"/>
                                    <TranslateTransform X="0" Y="0"/>
                                </TransformGroup>
                            </Grid.RenderTransform>
                            <!--<ScrollViewer x:Name="PART_ContentHost" />-->
                            <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                         Foreground="#FF6FB8FD"
                                         FontSize="24"
                                         Style="{StaticResource LostFocusStyle}"
                                         TextWrapping="Wrap"
                                         Text="{Binding .}"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         MinHeight="55"
                                         Cursor="Hand"
                                         IsReadOnly="True"
                                         FontFamily="Arial"
                                         Name="bar"
                                         >
                                <TextBox.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <LinearGradientBrush.RelativeTransform>
                                            <TransformGroup>
                                                <ScaleTransform CenterX="0.5" CenterY="0.5"/>
                                                <SkewTransform CenterX="0.5" CenterY="0.5"/>
                                                <RotateTransform CenterX="0.5" CenterY="0.5"/>
                                                <TranslateTransform/>
                                            </TransformGroup>
                                        </LinearGradientBrush.RelativeTransform>
                                        <GradientStop Color="#FF091F34" Offset="1"/>
                                        <GradientStop Color="#FF002F5C" Offset="0.4"/>
                                    </LinearGradientBrush>
                                </TextBox.Background>

                            </TextBox>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="bar" Property="Style" Value="{StaticResource MouseOverFocusStyle}" />
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="CustomListData" DataType="{x:Type ListBoxItem}">
        <Button Style="{StaticResource listButton}" />
    </DataTemplate>

  </Window.Resources>

  <Window.DataContext>
<ObjectDataProvider ObjectType="{x:Type local:ImageLoader}"  MethodName="LoadImages"  />
  </Window.DataContext>

    <ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79">

    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush>

        <Style TargetType="{x:Type ListBox}">
            <Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        </Style>

    </ListBox.Resources>

</ListBox>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml