2013-02-22 84 views
1

我已經爲lightswitch創建了以下自定義控件,並且如何訪問和獲取數據?如何訪問lightswitch中的自定義控件值

<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文件編碼: 在這裏,我增加值基於我的邏輯的組合框。

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 

然後我將該自定義控件添加到屏幕。 (首先創建屬性,然後指定自定義控制到)

enter image description here

當運行時,它會顯示如下 enter image description here

所以我的問題是如何訪問這兩個組合框,並得到其值?

我發現

Dim cmbyear As IContentItemProxy = Me.FindControl("StartYear") 

可以被用於訪問控制。但我怎麼能分別得到每個控制的價值?

回答

1

這可以按如下方式完成:首先必須創建本地屬性,並且在自定義控件中需要將它們與這些值綁定。

在我的方案中,我創建了兩個名爲StartYear/StartMonth的本地屬性。那麼在自定義控件中需要將它們綁定到SelectedItem並且模式必須是TwoWay。我做了如下:

<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> 

然後在我的xaml.vb代碼中可以直接訪問這些本地屬性。

相關問題