Reputation: 4933
i have create following custom control for lightswitch and how can i access and get data?
<UserControl x:Class="CustomControls.DateRange"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="75" d:DesignWidth="123">
<Grid x:Name="LayoutRoot" Background="White" Height="73">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,9,0,0" Name="cmbStartYear" VerticalAlignment="Top" Width="100" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="13,39,0,0" Name="cmbStartMonth" VerticalAlignment="Top" Width="99" />
</Grid>
</UserControl>
xaml.vb file coding : in here i added values for those combo boxes based on my logic.
Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim availableYears As List(Of Integer) = GetYears()
For Each year As Integer In availableYears
cmbStartYear.Items.Add(year)
Next
End Sub
then i add that custom control to screen. (first create property and then assign custom control to that)
when it runs it will display as below
so my question is how can i access these two combo boxes and get its value?
i found that
Dim cmbyear As IContentItemProxy = Me.FindControl("StartYear")
can be used to access control. but how i can i get value of each control separately?
Upvotes: 1
Views: 764
Reputation: 4933
this can be done as follows : first have to create local properties and in custom control need to bind them with those values.
in my scenario i have created two local properties called StartYear/StartMonth . then in custom control need to bind them to SelectedItem
and mode must be TwoWay
. i did it as follows:
<Grid x:Name="LayoutRoot" Background="White" Height="73">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,9,0,0" Name="cmbStartYear" VerticalAlignment="Top" Width="100" SelectedItem="{Binding Screen.StartYear, Mode=TwoWay}"/>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="13,39,0,0" Name="cmbStartMonth" VerticalAlignment="Top" Width="99" SelectedItem="{Binding Screen.StartMonth, Mode=TwoWay}" />
</Grid>
then within my xaml.vb code can directly access those local properties.
Upvotes: 1