WPF 精修篇 多值转换器
程序员文章站
2024-02-16 12:30:16
...
效果
贴码
首先需要一个多值转换器
class TextMulti:IMultiValueConverter
{
#region IMultiValueConverter 成员
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values[0] + " " + values[1] + " " + values[2];
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString().Split(' ');
}
#endregion
}
<Window.Resources>
<local:TextMulti x:Key="TextMulti"/>
</Window.Resources>
<Grid>
<TextBox x:Name="tex1" HorizontalAlignment="Left" Height="23" Margin="48,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="tex2" HorizontalAlignment="Left" Height="23" Margin="195,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="tex3" HorizontalAlignment="Left" Height="23" Margin="351,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="tex4" HorizontalAlignment="Left" Height="23" Margin="29,188,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="466">
<TextBox.Text>
<MultiBinding Converter="{StaticResource TextMulti}">
<Binding ElementName="tex1" Path="Text" Mode="TwoWay"/>
<Binding ElementName="tex2" Path="Text" Mode="TwoWay"/>
<Binding ElementName="tex3" Path="Text" Mode="TwoWay"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
</Grid>
死写法 活用就行~
充实的一天 晚上继续