如何在xaml中编写我的代码

时间:2017-12-12 15:14:33

标签: wpf vb.net xaml

是否可以在.xaml文件而不是.xaml.vb文件中编写以下代码?

Private Sub combx_Auswahl_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles combx_Auswahl.SelectionChanged
    If combx_Auswahl.SelectedIndex = 0 Then
        lbl_NeueMailAdresse.Visibility = Visibility.Visible
        txtbx_neueEmail.Visibility = Visibility.Visible
        btn_BestaetigeBearbeitung.IsEnabled = True
        btn_AbbruchBearbeitung.IsEnabled = True
    Else
        lbl_NeueMailAdresse.Visibility = Visibility.Hidden
        txtbx_neueEmail.Visibility = Visibility.Hidden
        btn_BestaetigeBearbeitung.IsEnabled = True
        btn_AbbruchBearbeitung.IsEnabled = True
    End If
End Sub

我的.xaml:

<Button x:Name="btn_BackToMainWindow" Content="zurück" HorizontalAlignment="Left" VerticalAlignment="Top" 
Width="150" Margin="37,539,0,0" Height="23"/>
<Button x:Name="btn_AbbruchBearbeitung" Content="abbrechen" HorizontalAlignment="Left" VerticalAlignment="Top" 
Width="150" Margin="205,460,0,0" Height="23"/>
<Button x:Name="btn_BestaetigeBearbeitung" Content="bestätigen" HorizontalAlignment="Left" Margin="37,460,0,0" VerticalAlignment="Top" 
Width="150" Height="23"/>
<ComboBox x:Name="combx_Auswahl" HorizontalAlignment="Left" Margin="37,219,0,0" VerticalAlignment="Top" Width="318"/>

1 个答案:

答案 0 :(得分:2)

如果所有控件都属于同一个名称范围,则可以使用带有数据触发器的样式:

<Window.Resources>
    <Style x:Key="style" TargetType="Control">
        <Setter Property="Visibility" Value="Hidden" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedIndex, ElementName=combx_Auswahl}" Value="0">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
        </Style.Triggers>

    </Style>

    <Style x:Key="buttonStyle" TargetType="Button">
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedIndex, ElementName=combx_Auswahl}" Value="0">
                <Setter Property="IsEnabled" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
...
<ComboBox x:Name="combx_Auswahl">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
</ComboBox>

<Label x:Name="lbl_NeueMailAdresse" Style="{StaticResource style}" />
<TextBox x:Name="txtbx_neueEmail" Style="{StaticResource style}" />
<Button Content="Button" x:Name="btn_BestaetigeBearbeitung" Style="{StaticResource buttonStyle}" />