2012-03-20 61 views
0

我想实现的是从RegistrationButton.cls类继承自Button的公共属性(因此我可以在文本块中设置绑定)。如何访问发件人自定义按钮值(从按钮继承的类)

所以我最终可以通过RegistrationButton的标题到下一页,所以我实际上知道按钮已被按下,并从那里进一步。

问题:

以下click事件发送者的类型按钮(因为它在我的XAML代码按钮声明)。所以问题在于,当我尝试将发件人转换为RegistrationButton时,发件人始终为NULL。而当我投的是一个按钮,我无法访问任何的RegistrationButton类。

所有按钮都保存在由RegistrationButton对象组成的ObversableCollection列表中。

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

一直在这个问题上挣扎很长一段时间,我不知道我到底需要改变什么。 xaml数据模板(如果是这样,究竟是什么?)或者后面的代码?

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

亲切的问候。

回答

1

如果它是一个RegistrationButton,那么也应该将它声明为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" /> 

注意的xmlns用于包括别名
使用的命名空间xmlns:my="clr-namespace:MyWpfProject"当它在当前的组装或
使用xmlns:my="clr-namespace:MyWpfProject;assembly=MyAssembly"如果RegistrationButton是在命名空间MyWpfProject名为MyAssembly程序另一个组件

编辑: 另外调整Style包括ding ControlTemplate相应地将TargetType设置为TargetType="{x:Type my:RegistrationButton}"

xmlns:my="clr-namespace=...也可以放在你的xaml的根目录下。

+0

你是什么意思与大会?当我使用my:标签时,只有我的页面才会显示类RegistrationButton。 – Jackz 2012-03-20 19:53:31

+0

@Jackz添加xmlns:my =“clr-namespace:** YOURNAMESPACE HERE **”并先构建它。比键入'<我的:'和intellisense应该填写其余的。否则,请告诉我RegistrationButton的命名空间。 – Silvermind 2012-03-20 19:56:33