【发布时间】:2018-02-15 09:47:17
【问题描述】:
我一直在尝试将两个文本框绑定到一个标签,以便标签始终根据两个文本框的内容进行更新。然而没有运气。我设法解决了如何绑定一个。
通过使用
Content="{Binding Text,ElementName=PersonName,UpdateSourceTrigger=PropertyChanged}"
原来是这样的
<UserControl x:Class="FitTracker.CreateTrackItem"
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:local="clr-namespace:FitTracker"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Name="PersonName" HorizontalAlignment="Left" Height="23" Margin="10,77,0,0" TextWrapping="Wrap" Text="Name" VerticalAlignment="Top" Width="280" />
<TextBox Name="PersonLevel" HorizontalAlignment="Left" Height="23" Margin="10,105,0,0" TextWrapping="Wrap" Text="Level" VerticalAlignment="Top" Width="280"/>
<Label Name="TrackDetails" Content="{Binding Text,ElementName=PersonName,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,133,0,0" VerticalAlignment="Top" Width="280" FontWeight="Bold" Background="#00000000" Foreground="White" />
</Grid>
</UserControl>
但是我不能用两个文本框来做。任何可以帮助我走上正确道路的想法或指南。
我已经搜索了几个小时了。
【问题讨论】:
-
请注意,设置
UpdateSourceTrigger=PropertyChanged在单向绑定中是没有意义的。它没有效果。 -
还请注意,虽然之前已经提出过这个问题,但answers here 都没有显示如何使用 MultiBinding 的 StringFormat 而不是 Converter。
标签: c# wpf data-binding