2017-02-22 20 views
0

Я создал библиотеку пользовательского контроля, чтобы создать пользовательский индикатор выполнения, который меняет цвет при достижении определенного значения и показывает в метке значение , все работает отлично, за исключением того, что индикатор выполнения не обновляет его, вот код. КонтрольWPF Custom ProgressBar в библиотеке управления пользователями не обновляется, но все остальные отлично работают

пользователя Control Library 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> 

    <local:CustomControl1 x:Name="ProgressBar1" Margin="10,10,10,0" VerticalAlignment="Top" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=mCurrentValue,UpdateSourceTrigger=Explicit}"/> 

    <Label x:Name="Label1" Content="{Binding Value, ElementName=ProgressBar1, Mode=TwoWay}" ContentStringFormat=" {0} %" HorizontalContentAlignment="Center" FontSize="14" Margin="212,34,204,41" BorderBrush="Transparent"/> 

</Grid> 
</UserControl> 

Пользователь библиотеки Code-Behind

Imports System.ComponentModel 
Public Class UserControl1 


Private mMaxValue As New Integer 


Public Shared ValueProperty As DependencyProperty = DependencyProperty.Register("mCurrentValue", GetType(Double), GetType(UserControl1), New PropertyMetadata(AddressOf OnStatusChanged)) 

Private Shared Sub OnStatusChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs) 
    CType(sender, UserControl1).ProgressBar1.Value = e.NewValue 
End Sub 

Public Property mCurrentValue As Double 
    Get 
     Return CDbl(GetValue(ValueProperty)) 
    End Get 
    Set(value As Double) 
     SetValue(ValueProperty, 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 
    End With 
End Sub 

Public Sub ChangeValue(ByVal value As Double) 
    mCurrentValue = value 
    Dim be As BindingExpression = ProgressBar1.GetBindingExpression(ProgressBar.ValueProperty) 
    be.UpdateSource() 
End Sub 

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

End Class 

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="461" Width="525" Opacity="2" Loaded="Window_Loaded"> 
<Grid Margin="0,0,0,-20"> 
    <ProgressBar x:Name="PrgBar" Margin="10,317,10,83" 
     Value="{Binding Value, ElementName=ProgressSlider}" 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="204,317,196,96" Content="0%" HorizontalContentAlignment="Center" FontSize="14" Height=" 38" /> 
    <Slider x:Name="ProgressSlider" Margin="10,10,10,276" Minimum="0" Maximum="100"/> 
    <Label x:Name="LblProva" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,44,0,0" Width="162" Height="132"/> 
    <sfjahgiga:UserControl1 x:Name="ProgressBarSF" HorizontalAlignment="Left" VerticalAlignment="Top" Height="106" Width="497" Margin="10,181,0,0"/> 
</Grid> 
</Window> 

WPF Application Code-Behind

Imports System.Windows.Threading 

Class MainWindow 

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() 
    With ProgressBarSF 
     LblProva.Content = "Colore1: " & .GetColor1() & vbCrLf & _ 
          "Colore2: " & .GetColor2() & vbCrLf & _ 
          "Colore3: " & .GetColor3() & vbCrLf & _ 
          "Valore Massimo: " & .GetMaxValue() & vbCrLf & _ 
          "Valore Corrente: " & .GetCurrentProgress() & vbCrLf & _ 
          "Colore Corrente: " & .GetCurrentColor() & vbCrLf 
    End With 
End Sub 

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) 
    writereports() 
End Sub 

End Class 

Заранее спасибо

ответ

1

решаемые путем удаления пользовательского элемента управления и использовать по умолчанию один