Reputation: 39
I want to catch an event when RadioButton is checked in a ListBox, I'm trying to use selectionChanged event but it was so delayed, and not effective. Is there any other way to do this? Thank you very much!
<ListBox SelectionChanged="lstPlotter_SelectionChanged_1" x:Name="lstPlotter" Style="{StaticResource 0009}">
<RadioButton Content="DWG To PDF.pc3" Style="{StaticResource 0004}" IsChecked="True"/>
<RadioButton Content="AutoCAD PDF (High Quality Print).pc3" Style="{StaticResource 0004}"/>
<RadioButton Content="AutoCAD PDF (General Documentation).pc3" Style="{StaticResource 0004}"/>
<RadioButton Content="AutoCAD PDF (Smallest File).pc3" Style="{StaticResource 0004}"/>
<RadioButton Content="AutoCAD PDF (Web and Mobile).pc3" Style="{StaticResource 0004}"/>
</ListBox>
Upvotes: 1
Views: 857
Reputation: 7908
I strongly advise you to learn how to separate Data and its Presentation (View).
The whole concept of WPF is built around this separation.
By not separating them, you create many problems for yourself.
In this task, your Data is a collection of strings contained in RadioButton.Content.
This collection of strings must be passed to the ListBox source.
In the template of the ListBox item, you need to pass the Data Template for the collection item from the source.
That is, for this case, for string.
It is in this Data Template that you need to set a RadioButton that will represent a string from the collection.
The RadioButton must be bound to the IsSelected property of the containing ListBoxItem.
<UniformGrid Background="AliceBlue" Columns="1">
<FrameworkElement.Resources>
<Style x:Key="0004"/>
<Style x:Key="0009"/>
<spec:StringCollection x:Key="ListBox.SourceItem">
<sys:String>DWG To PDF.pc3</sys:String>
<sys:String>AutoCAD PDF (High Quality Print).pc3</sys:String>
<sys:String>AutoCAD PDF (General Documentation).pc3</sys:String>
<sys:String>AutoCAD PDF (Smallest File).pc3</sys:String>
<sys:String>AutoCAD PDF (Web and Mobile).pc3</sys:String>
</spec:StringCollection>
<DataTemplate x:Key="ListBox.ItemTemplate"
DataType="{x:Type sys:String}">
<RadioButton GroupName="_listBox"
Content="{Binding}"
IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}"
Style="{DynamicResource 0004}"/>
</DataTemplate>
</FrameworkElement.Resources>
<TextBlock x:Name="tBlock"/>
<ListBox x:Name="lstPlotter"
SelectionChanged="lstPlotter_SelectionChanged_1"
Style="{DynamicResource 0009}"
ItemTemplate="{DynamicResource ListBox.ItemTemplate}"
ItemsSource="{DynamicResource ListBox.SourceItem}"
SelectedIndex="0"
SelectionMode="Single"/>
</UniformGrid>
private void lstPlotter_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
tBlock.Text = ((ListBox)sender).SelectedItem?.ToString();
}
Upvotes: 1