WPF UserControl库不会更新wpf项目中的gui

时间:2017-02-21 08:58:39

标签: wpf vb.net

大家好,我制作了一个带有进度条和标签的自定义控件库,该标签应该随着进度的变化自动更新,但标签内容不会改变,进度条gui不会更新,我该怎么办?

代码隐藏

Imports System.ComponentModel

Public Class UserControl1

Private WithEvents worker As BackgroundWorker

Private mMaxValue As New Integer

Public Property mCurrentValue As Double

Public Property GetCurrentValue As Double
    Get
        Return mCurrentValue
    End Get
    Set(value As Double)
        mCurrentValue = value
    End Set

End Property

Public Property mCurrentPrc As Integer

Public Property GetCurrentPrc As Integer
    Get
        Return mCurrentPrc
    End Get
    Set(value As Integer)
        mCurrentPrc = value
    End Set
End Property

Public Sub New()

    InitializeComponent()

    With ProgressBar1
        Dim Brush1 As New SolidColorBrush
        Brush1.Color = ConvertToRbg("#FF0000")
        .Resources.Add("BrushRed", Brush1)

        Dim Brush2 As New SolidColorBrush
        Brush2.Color = ConvertToRbg("#FFFF00")
        .Resources.Add("BrushYellow", Brush2)

        Dim Brush3 As New SolidColorBrush
        Brush3.Color = ConvertToRbg("#FF2EFF00")
        .Resources.Add("BrushGreen", Brush3)
    End With

End Sub

#Region "Functions"

Public Property GetColor1() As String

    Get
        With ProgressBar1
            Return .Resources("BrushRed").Color.ToString
        End With
    End Get
    Set(value As String)
        With ProgressBar1
            Dim Brush As New SolidColorBrush
            Brush.Color = ConvertToRbg(value)
            .Resources("BrushRed") = Brush
        End With
    End Set

End Property

Public Property GetColor2() As String
    Get
        With ProgressBar1
            Return .Resources("BrushYellow").Color.ToString
        End With
    End Get
    Set(value As String)
        With ProgressBar1
            Dim Brush As New SolidColorBrush
            Brush.Color = ConvertToRbg(value)
            .Resources("BrushYellow") = Brush
        End With
    End Set
End Property

Public Property GetColor3() As String
    Get
        With ProgressBar1
            Return .Resources("BrushGreen").Color.ToString
        End With
    End Get
    Set(value As String)
        With ProgressBar1
            Dim Brush As New SolidColorBrush
            Brush.Color = ConvertToRbg(value)
            .Resources("BrushGreen") = Brush
        End With
    End Set
End Property

Public Property GetMaxValue() As Integer
    Get
        Return mMaxValue
    End Get
    Set(value As Integer)
        mMaxValue = value
    End Set
End Property

Public Function GetCurrentColor()
    Return ProgressBar1.Foreground.ToString
End Function

Public Function GetCurrentProgress()
    Return ProgressBar1.Value.ToString
End Function

Public Function GetLabelContent()
    Return Label1.Content.ToString
End Function

Private Function ConvertToRbg(ByVal HexColor As String) As Color
    Dim Red As String
    Dim Green As String
    Dim Blue As String
    HexColor = Replace(HexColor, "#", "")
    Red = Val("&H" & Mid(HexColor, 1, 2))
    Green = Val("&H" & Mid(HexColor, 3, 2))
    Blue = Val("&H" & Mid(HexColor, 5, 2))
    Return Color.FromRgb(Red, Green, Blue)
End Function

#End Region

Public Sub ChangeColor()
    With ProgressBar1
        Dim prgrss As Double = .Value / 100

        Dim redbrsh As SolidColorBrush = .Resources("BrushRed")
        Dim grnbrsh As SolidColorBrush = .Resources("BrushGreen")
        Dim ylwbrsh As SolidColorBrush = .Resources("BrushYellow")


        If prgrss = 1D Then

            .Foreground = grnbrsh

        ElseIf prgrss >= 0.95D And prgrss < 1D Then

            .Foreground = ylwbrsh
        Else
            .Foreground = redbrsh
        End If

        Dim number As Integer = prgrss
    End With
End Sub

Public Sub ChangeValue(ByVal value As Double)
    GetCurrentValue = value

    GetCurrentPrc = value

End Sub

Private Sub ProgressBar1_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles ProgressBar1.ValueChanged
    ChangeColor()
End Sub

End Class

XAML

<UserControl
         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:sfjahgiga" x:Class="UserControl1" 
         mc:Ignorable="d" d:DesignWidth="540" Height="103">
<UserControl.Resources>

</UserControl.Resources>
<Grid DataContext="{Binding}">

    <local:CustomControl1 x:Name="ProgressBar1" Margin="10,10,10,0" VerticalAlignment="Top" Value="{Binding Path=mCurrentValue}"/>
    <Label Name="Label1" Margin="250,32,250,37" Content="{Binding Path=mCurrentPrc}" />

</Grid>
</UserControl>

我如何在wpf项目中实现它

Private Sub progress_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles ProgressSlider.ValueChanged

    Dim valore As Integer = ProgressSlider.Value

    ProgressBarSF.GetMaxValue() = 100
    ProgressBarSF.ChangeValue(valore)
    Dim perc As Integer = valore

    LblProgress.Content = perc & "%"
    Dim prgrss As Double = valore / 100

    Dim redbrsh As SolidColorBrush = PrgBar.Resources("BrushRed")
    Dim grnbrsh As SolidColorBrush = PrgBar.Resources("BrushGreen")
    Dim ylwbrsh As SolidColorBrush = PrgBar.Resources("BrushYellow")

    If prgrss = 1D Then

        PrgBar.Foreground = grnbrsh

    ElseIf prgrss >= 0.95D And prgrss < 1D Then

        PrgBar.Foreground = ylwbrsh
    Else
        PrgBar.Foreground = redbrsh
    End If

    writereports()
end sub

Public Sub writereports() 'checking if everything works fine
    With ProgressBarSF
        LblProva.Content = "Colore1: " & .GetColor1() & vbCrLf & _
                            "Colore2: " & .GetColor2() & vbCrLf & _
                            "Colore3: " & .GetColor3() & vbCrLf & _
                            "Valore Massimo: " & .GetMaxValue() & vbCrLf & _
                            "Valore Corrente: " & .GetCurrentValue() & vbCrLf & _
                            "Colore Corrente: " & .GetCurrentColor() & vbCrLf & _
                            "Contenuto Label: " & .GetLabelContent()
    End With
End Sub

wpf项目XAML

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
xmlns:sfjahgiga="clr-namespace:sfjahgiga;assembly=sfjahgiga" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525" Opacity="2">
<Grid>
    <ProgressBar x:Name="PrgBar" Margin="20,181,20,101"
        Value="{Binding Value, ElementName=ProgressSlider}" Height="38" IsIndeterminate="False" Background="#d3d3d3" Foreground="{DynamicResource BrushYellow}">
        <ProgressBar.Resources>
            <SolidColorBrush x:Key="BrushRed" Color="#FFFF0000"/>
            <SolidColorBrush x:Key="BrushGreen" Color="#FF2EFF00"/>
            <SolidColorBrush x:Key="BrushYellow" Color="#FFFF00"/>
        </ProgressBar.Resources>
    </ProgressBar>
    <Label x:Name="LblProgress" Margin="205,185,195,97" Content="0%" HorizontalContentAlignment="Center" FontSize="14" Height=" 38" />
    <Slider x:Name="ProgressSlider" Margin="10" Minimum="0" Maximum="100"/>
    <sfjahgiga:UserControl1 x:Name="ProgressBarSF" HorizontalAlignment="Left" Margin="0,224,0,0" VerticalAlignment="Top" Height="91"  />
    <Label x:Name="LblProva" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,44,0,0" Width="162" Height="132"/>


</Grid>
</Window>

编辑1

使用实际代码,它不会更新任何内容,无论是视觉还是值

0 个答案:

没有答案