2012-03-20 64 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