我可以将数据源绑定(bind)到显示文本的文本 block 。但是,如果选中了复选框 foo 的值,我想将 Fontweight 设置为粗体。我正在尝试使用 IMultiValueConverter 来完成此任务,但到目前为止还没有运气。知道我做错了什么吗?
<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>
和转换器类(只是硬连线以始终返回粗体)
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
最佳答案
如果您想使用转换器,您应该绑定(bind)到 CheckBox.IsChecked
({Binding IsChecked, ElementName=foo}
),您不需要 MultiBinding
,然后在 Convert
中将 value
转换为 bool
并根据返回正常或粗体(最好作为实际值) FontWeight
,而不是字符串
)。
但是在这里我会推荐 DataTrigger关于IsChecked
。
例如
<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>
(另请注意 dependency property precedence ,如果您在本地设置 FontWeight,触发器将不会执行任何操作)
关于.net - 如何在同一个 WPF 控件上绑定(bind)多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8838925/