VB에서 내 자신의 NumericUpDown 컨트롤을 만들고 있습니다. 뒤에 VB 코드 여기VB.net의 WPF NumericUpDown usercontrol
<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>
그리고 :
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"
여기 내 XAML입니다
콘텐츠 섹션에 넣으세요.
<local:NumericUpDown Numeric="{Binding Path=score, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
물론 컨테이너에 DataContext를 이미 설정했으며 다른 모든 데이터 바인딩 된 컨트롤은 정상적으로 작동합니다. 하지만 사용자 지정 컨트롤의 텍스트 상자가 비어 있습니다. 여기서 끝나지 않습니다. 내가 입력란에 텍스트를 입력 할 때 값을 줄이고 증가 버튼을 사용하면 값이 내 DataTable로 전송됩니다. 즉,이 usercontrol은 일부 확장 작업을 의미합니다. 나는 어디에서 잘못 했는가? 텍스트 상자의 내용을 시작 값으로 초기화하지 않는 이유는 무엇입니까?
조금 더 테스트 한 후. 내 usercontrol '양방향'에서 작동하지 않는 것 같습니다. DataTable에서 데이터를받지 못하면 값만 전달됩니다. 어떻게 수정해야합니까?
관련없는, 그리고 주로 의견을 기반으로 한 의견 : 'NumericUpDown' ** sucks. ** WPF에없는 주된 이유 일 것입니다. 버튼이 너무 작아 클릭하기 어렵고 터치 장치에서 완전히 사용할 수 없습니다. MouseWheel 클래스 이벤트 핸들러를 등록하고이를 기반으로 값을 늘리거나 줄여서 더 나을 것입니다. 그냥 제안. –