Skip to content

Instantly share code, notes, and snippets.

@BYJRK
Created June 28, 2023 13:41
Show Gist options
  • Save BYJRK/40178d19192cd2ed5876b029e18049e9 to your computer and use it in GitHub Desktop.
Save BYJRK/40178d19192cd2ed5876b029e18049e9 to your computer and use it in GitHub Desktop.
ValueConverters.NET 工具包简单演示代码
<Window x:Class="WpfTutorials.NuGetValueConverters.Views.MainWindow"
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"
xmlns:conv="clr-namespace:ValueConverters;assembly=ValueConverters"
Title="登录"
Width="800"
Height="600"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Window.Resources>
<conv:BoolToVisibilityConverter x:Key="AgreementToVisibilityConverter" IsInverted="True" />
<conv:ValueConverterGroup x:Key="UserNameToVisibilityConverter">
<conv:StringIsNotNullOrEmptyConverter />
<conv:BoolInverter />
<conv:BoolToVisibilityConverter />
</conv:ValueConverterGroup>
<conv:ValueConverterGroup x:Key="PasswordLengthToVisibilityConverter">
<conv:IsInRangeConverter MinValue="8" MaxValue="999" />
<conv:BoolInverter />
<conv:BoolToVisibilityConverter />
</conv:ValueConverterGroup>
<conv:ValueConverterGroup x:Key="AgeToVisibilityConverter">
<conv:StringToDecimalConverter />
<conv:IsInRangeConverter MinValue="18" MaxValue="99" />
<conv:BoolInverter />
<conv:BoolToVisibilityConverter />
</conv:ValueConverterGroup>
</Window.Resources>
<Grid>
<Border Padding="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
CornerRadius="5"
BorderBrush="Gray"
Background="White"
TextElement.FontSize="18"
BorderThickness="1">
<Border.Effect>
<DropShadowEffect />
</Border.Effect>
<Border.Resources>
<Style TargetType="TextBox">
<Setter Property="Width" Value="150" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Width" Value="75" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="CheckBox">
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</Border.Resources>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock>用户名:</TextBlock>
<TextBox Name="username" />
</StackPanel>
<StackPanel Margin="0,5,0,0" Orientation="Horizontal">
<TextBlock>年龄:</TextBlock>
<TextBox Name="userage" Text="14" />
</StackPanel>
<StackPanel Margin="0,5,0,0" Orientation="Horizontal">
<TextBlock>密码:</TextBlock>
<TextBox Name="password" />
</StackPanel>
<CheckBox Name="agree" Margin="0,5,0,0" Content="我已阅读并同意用户协议" />
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="0,5,0,0" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Foreground" Value="Red" />
</Style>
</StackPanel.Resources>
<TextBlock Visibility="{Binding ElementName=username, Path=Text, Converter={StaticResource UserNameToVisibilityConverter}}">用户名不能为空!</TextBlock>
<TextBlock Visibility="{Binding ElementName=userage, Path=Text, Converter={StaticResource AgeToVisibilityConverter}}">年龄需要在18~99之间!</TextBlock>
<TextBlock Visibility="{Binding ElementName=password, Path=Text.Length, Converter={StaticResource PasswordLengthToVisibilityConverter}}">密码长度不能小于8位!</TextBlock>
<TextBlock Visibility="{Binding ElementName=agree, Path=IsChecked, Converter={StaticResource AgreementToVisibilityConverter}}">您需要勾选同意用户协议!</TextBlock>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment