美文网首页
[WPF] 实现根据ComboBox选项设定其他控件使能(Ena

[WPF] 实现根据ComboBox选项设定其他控件使能(Ena

作者: zhongwcool | 来源:发表于2020-06-28 17:53 被阅读0次

为建立中文知识库加块砖        ——中科大胡不归

0. 前言

在实现类似设置页面,常需要实现某组设置项依赖某个属性。比如父项是 ToggleButton,打开开关,可以设定子项的参数。关闭开关,子项被 Disable 而无法编辑。这种情况,简单的 Binding 就可以实现。

还有种更复杂的情况,父项是 ComboBox,有多个选项,其中一个选项对应使能开关。这种情况我们需要用到 Converter。

学习WPF: 第五个月。

1. View代码

<UserControl x:Class="HelloMvvmLight.View.MainDrawer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:hc="https://handyorg.github.io/handycontrol"
             xmlns:converter="clr-namespace:HelloMvvmLight.Converter"
             mc:Ignorable="d"
             x:Name="MyDrawer"
             DataContext="{Binding Main, Source={StaticResource Locator}}"
             d:DesignWidth="400">
    <Grid Margin="5" Background="White">
        <Grid.Resources>
            <Style x:Key="TitleText" TargetType="TextBlock">
                <Setter Property="Foreground" Value="Gray" />
                <Setter Property="Padding" Value="5,0,0,0" />
                <Setter Property="VerticalAlignment" Value="Center"></Setter>
                <Setter Property="FontFamily" Value="微软雅黑,SimSun" />
            </Style>
            
            <converter:LangInfo2BoolConverter x:Key="Lang2Bool"/>
        </Grid.Resources>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        
        <DockPanel Grid.Row="4" Margin="0,0,0,15">
            <TextBlock DockPanel.Dock="Left" Style="{StaticResource TitleText}" Text="下拉演示" Width="60"></TextBlock>
            <ComboBox x:Name="ComboLang" ItemsSource="{Binding LangList}" SelectedItem="{Binding Path=CurrentLang}" 
                      DisplayMemberPath="Name" />
        </DockPanel>
        
        <DockPanel Grid.Row="5" Margin="0,0,0,15">
            <TextBlock DockPanel.Dock="Left" Style="{StaticResource TitleText}" Text="数字演示" Width="60"></TextBlock>
            <hc:NumericUpDown IsEnabled="{Binding SelectedValue, ElementName=ComboLang, Converter={StaticResource Lang2Bool}}" 
                              Margin="0,0,0,0" Style="{StaticResource NumericUpDownPlus}"/>
        </DockPanel>
    </Grid>
</UserControl>

注意:需要在resource中声明我们要用的Converter。

<Grid.Resources>
            <converter:LangInfo2BoolConverter x:Key="Lang2Bool"/>
</Grid.Resources>

2. Converter代码

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using HelloMvvmLight.Model;

namespace HelloMvvmLight.Converter
{
    public class LangInfo2BoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(Boolean)) {
                if (null != value && value is LangInfo item)
                {
                    switch (item.Value)
                    {
                        case "default":
                        {
                            return false;
                        }

                        default:
                        {
                            return true;
                        }
                    }
                }

                return true;
            }
            throw new InvalidOperationException("Converter can only convert to value of type Boolean.");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

3. 效果演示

参考文章:

  1. Binding to a WPF ToggleButton's IsChecked state
  2. How do you bind the TextWrapping property of a TextBox to the IsChecked value of a MenuItem?

相关文章

网友评论

      本文标题:[WPF] 实现根据ComboBox选项设定其他控件使能(Ena

      本文链接:https://www.haomeiwen.com/subject/rzuwfktx.html