简体   繁体   English

WPF IValueConverter 未触发

[英]WPF IValueConverter not firing

I'm attempting to implement a value converter that will change the color of a button on a user control based on the custom "MyUserControlStatus" property of the user control "MyUserControl".我正在尝试实现一个值转换器,它将根据用户控件“MyUserControl”的自定义“MyUserControlStatus”属性更改用户控件上按钮的颜色。

The code-behind looks like this:代码隐藏看起来像这样:

Public Class MyUserControl
    Public Property MyUserControlStatus As Integer = 0
End Class

Public Class StatusIndicatorConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        Dim indicatorbrush As Brush = Brushes.Transparent
        Dim status As Integer = CType(value, Integer)
        Select Case status
            Case 0 : indicatorbrush = Brushes.Red
            Case 1: indicatorbrush = Brushes.Green
            Case 2 : indicatorbrush = Brushes.Yellow
        End Select

        Return indicatorbrush
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

The XAML looks like this: XAML 看起来像这样:

<UserControl x:Class="MyUserControl"
             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:MainApplicationName"
             xmlns:wpf="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
             mc:Ignorable="d"
             >

    <UserControl.Resources>
        <local:StatusIndicatorConverter x:Key="MyStatusIndicatorConverter"/>
    </UserControl.Resources>

                        <Button x:Name="ButtonStatusIndicator"
                                Background="{Binding Path=MyUserControlStatus, Converter={StaticResource MyStatusIndicatorConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                >
                            <Button.Resources>
                                <Style TargetType="Border">
                                    <Setter Property="Height" Value="10"/>
                                    <Setter Property="Width" Value="10"/>
                                    <Setter Property="CornerRadius" Value="10"/>
                                </Style>
                            </Button.Resources>
                        </Button>

</UserControl>

I get an empty, non-colored status.我得到一个空的、非彩色的状态。 The breakpoints within the Convert function don't fire. Convert function 中的断点不会触发。 I'm not sure what I'm doing wrong here, as all the parts seem to be in place.我不确定我在这里做错了什么,因为所有部分似乎都已到位。 It should have made the button indicator red (for value=0).它应该使按钮指示器变为红色(值=0)。 The expectation is that, as the MyUserControlStatus changes, the color of the indicator button will change, too, as it is bound to MyUserControlStatus and that value is converted to a color.预期是,随着 MyUserControlStatus 的变化,指示器按钮的颜色也会发生变化,因为它绑定到 MyUserControlStatus 并且该值被转换为颜色。

A Binding in a UserControl's XAML to one of its own properties must explicitly set the source object of the Binding, eg by setting the RelativeSource property. UserControl 的 XAML 中的绑定到它自己的属性之一必须显式设置绑定的源 object,例如通过设置 RelativeSource 属性。

Setting Mode=TwoWay and UpdateSourceTrigger=PropertyChanged is however pointless.然而,设置Mode=TwoWayUpdateSourceTrigger=PropertyChanged是没有意义的。 It has no effect at all in this Binding.它在这个 Binding 中完全没有作用。

Background="{Binding Path=MyUserControlStatus,
                     RelativeSource={RelativeSource AncestorType=UserControl},
                     Converter={StaticResource MyStatusIndicatorConverter}}"

Besides that, the source property must fire a change notification in order to have the binding target property updated.除此之外,源属性必须触发更改通知才能更新绑定目标属性。

It should be implemented as a dependency property :它应该作为依赖属性实现:

Public Shared ReadOnly MyUserControlStatusProperty As DependencyProperty =
    DependencyProperty.Register(
        name:="MyUserControlStatus",
        propertyType:=GetType(Integer),
        ownerType:=GetType(MyUserControl))

Public Property MyUserControlStatus As Integer
    Get
        Return CType(GetValue(MyUserControlStatusProperty), Integer)
    End Get
    Set
        SetValue(MyUserControlStatusProperty, Value)
    End Set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM