為建立中文知識庫加塊磚 ——中科大胡不歸
0. 前言
在實現(xiàn)類似設置頁面脱茉,常需要實現(xiàn)某組設置項依賴某個屬性陈惰。比如父項是 ToggleButton,打開開關纤掸,可以設定子項的參數(shù)。關閉開關浑塞,子項被 Disable 而無法編輯借跪。這種情況,簡單的 Binding 就可以實現(xiàn)酌壕。
還有種更復雜的情況掏愁,父項是 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="數(shù)字演示" 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();
}
}
}