[英]How can I bind multiple properties on the same WPF control?
I am able to bind my datasource to the textblock for the display text. 我能够将我的数据源绑定到显示文本的文本块。 However I would like to set the Fontweight to bold if the value of the checkbox foo is checked. 但是,如果选中了复选框foo的值,我想将Fontweight设置为粗体。 I'm trying to use IMultiValueConverter to accomplish this, but have had no luck so far. 我正在尝试使用IMultiValueConverter来完成此操作,但到目前为止还没有运气。 Any idea as to what I'm doing wrong? 关于我在做什么错的任何想法吗?
<CheckBox Name="foo"/>
<TextBlock Name="bar" Text="{Binding Path=Name}">
<TextBlock.FontWeight>
<MultiBinding Converter="{StaticResource FontConverter}">
<Binding RelativeSource="{RelativeSource self}" Path="???"/>
<Binding ElementName="???" />
</MultiBinding>
</TextBlock.FontWeight>
</TextBlock>
and the converter class (just hardwired to always return bold for now) 和转换器类(只是硬连接到现在总是返回粗体)
Public Class FontConverter
Implements IMultiValueConverter
Public Function Convert(values() As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
Return "Bold"
End Function
Public Function ConvertBack(value As Object, targetTypes() As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Return nothing
End Function
End Class
If you want to use a converter you should bind to the CheckBox.IsChecked
( {Binding IsChecked, ElementName=foo}
), you do not need a MultiBinding
, then in Convert
cast the value
to bool
and based on that either return normal or bold (preferably as an actual FontWeight
, not a string
). 如果要使用转换器,则应绑定到CheckBox.IsChecked
( {Binding IsChecked, ElementName=foo}
),不需要MultiBinding
,然后在Convert
中将value
MultiBinding
Convert
为bool
并根据该value
返回normal或bold (最好是实际的FontWeight
,而不是string
)。
Here however i would recommend a DataTrigger on IsChecked
. 但是在这里我建议在IsChecked
上使用DataTrigger 。
eg 例如
<TextBlock Text="{Binding Name}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=foo}"
Value="true">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger >
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
(Also note dependency property precedence , if you set the FontWeight locally the trigger will no do anything) (还请注意依赖项属性优先级 ,如果您在本地设置FontWeight,则触发器将不执行任何操作)
Anything based on a condition in XAML should be done in a Trigger
or DataTrigger
. 基于XAML中条件的任何操作都应在Trigger
或DataTrigger
。 Converters
should be used when converting values from one value to another. 将值从一个值转换为另一个值时,应使用转换Converters
。
Here's an example: 这是一个例子:
<CheckBox Name="foo"/>
<TextBlock Name="bar" Text="{Binding Path=Name}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=foo, Path=IsChecked}" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.