Use Linq to get control from a container : UI Element « Windows Presentation Foundation « VB.Net






Use Linq to get control from a container

Use Linq to get control from a container
      

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" SizeToContent="Height" Width="300">
    <Grid Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" BorderBrush="Gray" BorderThickness="1" />
        <Border Grid.Column="1" BorderBrush="Gray" BorderThickness="1" />
        <StackPanel Grid.Column="0" HorizontalAlignment="Center" Margin="5" Name="spLeftContainer">
            <TextBlock FontSize="16" Text="Radio Group 1" />
            <RadioButton Content="Radio Button 1A" GroupName="Group1" IsChecked="True" Margin="5" Name="rbnOneA" />
            <RadioButton Content="Radio Button 1B" GroupName="Group1" Margin="5" Name="rbnOneB" />
            <RadioButton Content="Radio Button 1C" GroupName="Group1" Margin="5" Name="rbnOneC" />
            <Separator/>
            <TextBlock FontSize="16" Text="Radio Group 2" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2A" IsChecked="True" 
                         Margin="5" Name="rbnTwoA" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2B" Margin="5" Name="rbnTwoB"/>
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2C" Margin="5" Name="rbnTwoC"/>
        </StackPanel>
        <StackPanel Grid.Column="1" HorizontalAlignment="Center" Margin="5" Name="spRightContainer">
            <TextBlock FontSize="16" Text="Radio Group 1" />
            <RadioButton Content="Radio Button 1D" GroupName="Group1" Margin="5" Name="rbnOneD" />
            <RadioButton Content="Radio Button 1E" GroupName="Group1" Margin="5" Name="rbnOneE" />
        </StackPanel>
        <Button Content="Show Group1 Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center" 
                Margin="10" MaxHeight="25" Click="Button_Click" />
    </Grid>
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
      Dim radioButton As RadioButton = Nothing
      radioButton = GetCheckedRadioButton(spLeftContainer.Children, "Group1")
      If radioButton Is Nothing Then
        radioButton = GetCheckedRadioButton(spRightContainer.Children, "Group1")
      End If
      MessageBox.Show(Convert.ToString(radioButton.Content) & " checked.", Title)
    End Sub
    Private Function GetCheckedRadioButton(children As UIElementCollection, groupName As [String]) As RadioButton
      Return children.OfType(Of RadioButton)().FirstOrDefault(Function(rb) rb.IsChecked = True AndAlso rb.GroupName = groupName)
    End Function
    Private Sub RadioButton_Checked(sender As Object, e As RoutedEventArgs)
      If Not Me.IsInitialized Then
        Return
      End If
      Dim radioButton As RadioButton = TryCast(e.OriginalSource, RadioButton)

      If radioButton IsNot Nothing Then
        MessageBox.Show(Convert.ToString(radioButton.Content) & " checked.", Title)
      End If
    End Sub
  End Class
End Namespace

   
    
    
    
    
    
  








Related examples in the same category

1.Find enclosure componentFind enclosure component
2.Clear locally set values and restore the default values of dependency propertiesClear locally set values and restore the default values of dependency properties
3.PresentationTraceSources.SetTraceLevel(binding,PresentationTraceLevel.High);PresentationTraceSources.SetTraceLevel(binding,PresentationTraceLevel.High);
4.UI Element Mouse Clicked EventsUI Element Mouse Clicked Events
5.Programmatically Extract an Element's Style with DefaultStyleKeyPropertyProgrammatically Extract an Element's Style with DefaultStyleKeyProperty
6.Print Logical TreePrint Logical Tree
7.Print Visual TreePrint Visual Tree
8.About Dialog with Tree WalkingAbout Dialog with Tree Walking
9.Changing graphical elementsChanging graphical elements
10.UIElement CountUIElement Count
11.Find inner visual elementFind inner visual element
12.Logical Visual Tree SampleLogical Visual Tree Sample
13.Point Hit Test with VisualTreeHelper.HitTestPoint Hit Test with VisualTreeHelper.HitTest
14.Change the Visibility property of a UIElement.Change the Visibility property of a UIElement.
15.Mixing graphics with Control ElementsMixing graphics with Control Elements