Чего я хочу добиться, так это получить доступ к открытым свойствам из класса RegistrationButton.cls, который наследуется от Button (чтобы я мог установить привязку в текстовых блоках).
Таким образом, я могу в конечном итоге передать заголовокRegistrationButton на следующую страницу, так что я на самом деле знаю, какая кнопка была нажата, и перенесу ее оттуда.
Проблема:
Следующий отправитель события щелчка имеет тип button (поскольку она объявлена как кнопка в моем коде xaml).Так вот в чем проблема, отправитель всегда равен NULL, когда я пытаюсь привести отправителя к элементу RegistrationButton.И когда я выполняю приведение в качестве кнопки, я не могу получить доступ к чему-либо из класса RegistrationButton.
Все кнопки сохраняются в списке ObversableCollection, состоящем из объектов RegistrationButton.
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>
С уважением.