简体   繁体   中英

How to access sender custom button values (Class inherited from button)

What i want to achieve is to access public properties from the RegistrationButton.cls class that inherits from Button (so i can set the binding in the textblocks).

So i can eventually pass the Title of the RegistrationButton to a next page so i actually know what button has been pressed and take it further from there.

Problem:

The following click event sender is of the type button (since its declared as a button in my xaml code). So here is the problem, the sender is always NULL when i try to cast the sender to a RegistrationButton. And when i cast is as a Button i cannot access anything of the RegistrationButton class.

All the buttons get saved in a ObversableCollection list consisting of RegistrationButton objects.

  private void RegistrationButton_Click(object sender, RoutedEventArgs e)
    {
        RegistrationButton b = sender as RegistrationButton;
        String buttonTitle = b.Title;
    }

Been struggling with this problem for a long time now and i have no clue what exactly i have to change. The xaml data template (if so, how exactly?) or the code behind?

XAML:

<ListBox  x:Name="lbRegistration" ItemsSource="{Binding RegBtns, ElementName=Window}" Background="{x:Null}" 
            BorderBrush="{x:Null}" Grid.Column="1" 
            ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="75">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148"
                            Style="{DynamicResource ButtonStyleRegistration}"
                            Margin="10,0,5,0" 
                            Click="RegistrationButton_Click" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Style of the button:

        <Style x:Key="ButtonStyleRegistration" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="registrationButton">
                        <Rectangle Fill="#FF89959A" Height="Auto" RadiusY="15" RadiusX="15" Stroke="White" Width="Auto"/>

                        <TextBlock x:Name="tbOorzaak" TextWrapping="Wrap" 
                                   Text="{Binding RegistrationCount, StringFormat=Cause: \{0\}}"
                                   HorizontalAlignment="Center" Margin="7.5,7.5,0,0" Height="Auto" 
                                   VerticalAlignment="Top" FontWeight="Bold" >
                        </TextBlock>
                        <TextBlock x:Name="tbDuurStilstand" TextWrapping="Wrap" 
                            Text="{Binding RegistrationCount, StringFormat= \{0\}}" 
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center" 
                            Margin="7.5,5,0,0" Height="24.8266666666667"/>
                        <TextBlock x:Name="tbBeginStilstand" 
                            Text="{Binding RegistrationCount, StringFormat= \{0\}}"
                            TextWrapping="Wrap" Margin="7.5,0,0,7.5" 
                            VerticalAlignment="Bottom" d:LayoutOverrides="Width" 
                            HorizontalAlignment="Center" Height="Auto"/>

                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize" Value="10.667"/>
    </Style>  

Kind regards.

If it's a RegistrationButton than you should also declare it as a RegistrationButton.

<my:RegistrationButton xmlns:my="clr-namespace:MyWpfProject" x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148" 
                            Style="{DynamicResource ButtonStyleRegistration}" 
                            Margin="10,0,5,0"  
                            Click="RegistrationButton_Click" /> 

Note the xmlns for including the namespace with alias my
use xmlns:my="clr-namespace:MyWpfProject" when it is in your current assembly or
use xmlns:my="clr-namespace:MyWpfProject;assembly=MyAssembly" if the RegistrationButton is in the namespace MyWpfProject in another assembly named MyAssembly

Edit: In addition adjust the Style including the ControlTemplate accordingly setting the TargetType to TargetType="{x:Type my:RegistrationButton}"

the xmlns:my="clr-namespace=... can also be placed in the root of your xaml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM