簡體   English   中英

WP.net中的WPF NumericUpDown用戶控件

[英]WPF NumericUpDown usercontrol in VB.net

我在VB中創建自己的NumericUpDown控件。 這是我的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" 
             x:Class="NumericUpDown"
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="100">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <TextBox x:Name="txtNum" Grid.Column="0" x:FieldModifier="private" TextChanged="txtNum_TextChanged"/>
        <Button x:Name="cmdDown" Grid.Column="1" x:FieldModifier="private" Content="˅" Width="20" Click="cmdDown_Click" />
        <Button x:Name="cmdUp" Grid.Column="2" x:FieldModifier="private" Content="˄" Width="20" Click="cmdUp_Click" />
    </Grid>
</UserControl>

這是它背后的VB代碼:

Class NumericUpDown

    Dim _Minimum As Double = 0
    Dim _Maximum As Double = 100

    Private Sub NumericUpDown()
        InitializeComponent()
        txtNum.Text = Numeric
    End Sub

    Public Property Maximum As Double
        Get
            Return _Maximum
        End Get
        Set(value As Double)
            _Maximum = value
        End Set
    End Property

    Public Property Minimum As Double
        Get
            Return _Minimum
        End Get
        Set(value As Double)
            _Minimum = value
        End Set
    End Property

    Public Shared ReadOnly NumericProperty As DependencyProperty = DependencyProperty.Register("Numeric", GetType(String), GetType(NumericUpDown), _
        New PropertyMetadata(""))

    Public Property Numeric As String
        Get
            Return CType(GetValue(NumericProperty), String)
        End Get
        Set(value As String)
            SetValue(NumericProperty, value)
        End Set
    End Property

    Private Sub cmdUp_Click(sender As Object, e As RoutedEventArgs)
        Dim NumValue As Double
        NumValue = Val(txtNum.Text)
        NumValue += 1
        If NumValue > Maximum Then NumValue = Maximum
        txtNum.Text = NumValue.ToString
    End Sub

    Private Sub cmdDown_Click(sender As Object, e As RoutedEventArgs)
        Dim NumValue As Double
        NumValue = Val(txtNum.Text)
        NumValue -= 1
        If NumValue < Minimum Then NumValue = Minimum
        txtNum.Text = NumValue.ToString
    End Sub

    Private Sub txtNum_TextChanged(sender As Object, e As TextChangedEventArgs)
        Numeric = txtNum.Text
    End Sub
End Class

我在我的頁面中使用它像這樣:把它放在頁面定義上:

xmlns:local="clr-namespace:Demo"

並將其放在內容部分:

<local:NumericUpDown Numeric="{Binding Path=score, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>

當然我已經在容器上設置了DataContext,所有其他數據綁定控件都可以正常工作。 但我的自定義控件中的文本框變空了! 它並沒有在這里結束。 當我在文本框中輸入內容時,當我使用減少和增加按鈕給它一些值時,該值將傳輸到我的DataTable; 這意味着這個用戶控件在某種程度上可以工作。 我哪里做錯了? 為什么不使用起始值初始化Textbox內容?

經過多一點測試。 似乎我的usercontrol不能在'雙向'中工作。 它不接收來自DataTable的數據,它只傳播值給它。 我如何解決它?

問題是您沒有將txtNumText屬性txtNum到您的Numeric屬性。

<TextBox x:Name="txtNum" Grid.Column="0" x:FieldModifier="private"
            Text="{Binding Path=Numeric, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>

然后你可以刪除txtNum_TextChanged ,並修改你的上/下點擊處理程序只是更新Numeric屬性,例如:

Private Sub cmdUp_Click(sender As Object, e As RoutedEventArgs)
    If Me.Numeric < Me.Maximum Then
        Me.Numeric += 1
    Else
        Me.Numeric = Me.Maximum
    End If
End Sub

Private Sub cmdDown_Click(sender As Object, e As RoutedEventArgs)
    If Me.Numeric > Me.Minimum Then
        Me.Numeric -= 1
    Else
        Me.Numeric = Me.Minimum
    End If
End Sub

請注意,仍然存在許多問題 - 用戶可以輸入超出允許范圍的值,或非數字數據(這將破壞事情!)等。對於此特定問題,您可以查看擴展EPF工具包 ,它有各種上/下控制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM