오히려 슬라이더의 기본 동작과 TwoWay
바인딩에 의존하지 않고, 다음 초에 처음 "SliderValueProperty"에, 당신의 SelectedItemProperty
세터의 새로운 최대 값을 계산 한 후 새로운 최대를 사용하여 PropertyChanged
이벤트를 발생하고, "MaxValueProperty". 개인 소유의 백킹 필드에 대한 계산은 공개 속성이 아니라 수행하십시오.
이렇게하면 시스템이 먼저 슬라이더 값을 조정 한 다음 maxvalue를 조정하여 슬라이더 위치를 이동해야합니다.
slider1의 바인딩 모드를 Maximum
에서 OneWay
으로 설정합니다. 나는 그것이 트릭을 할 것이라고 생각한다. 이 같은 뷰 모델로 다음과 같이
는 :
Class viewmodel
Implements INotifyPropertyChanged
Private _selectedItemProperty As ComboBoxItem
Public Property SelectedItemProperty As ComboBoxItem
Get
Return _selectedItemProperty
End Get
Set(value As ComboBoxItem)
MaxValueProperty = value.Content
If _sliderValueProperty > _maxValueProperty Then _sliderValueProperty = _maxValueProperty
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MaxValueProperty"))
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("SliderValueProperty"))
End Set
End Property
Private _maxValueProperty, _sliderValueProperty as Single
Public Property MaxValueProperty As Single ' with setter that will raise PropertyChanged and use the backing field
'...
Public Property SliderValueProperty As Single ' with a setter that will raise PropertyChanged and use the backing field
'...
End Class
그리고 XAML은 다음과 같이 조금 보이는 : 당신이 대답 직전에
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="40,38,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="120" SelectedItem="{Binding SelectedItemProperty}">
<ComboBoxItem Content="50" />
<ComboBoxItem Content="30" />
<ComboBoxItem Content="10" />
</ComboBox>
<Slider Name="slider1" Height="23" HorizontalAlignment="Left" Margin="87,135,0,0" VerticalAlignment="Top" Width="100" Value="{Binding SliderValueProperty}" Minimum="1" Maximum="{Binding MaxValueProperty}" />
<Label Content="{Binding ElementName=slider1, Path=Value}" Margin="40,174,293,96" />
</Grid>
을, 나는 문 만약 당신 유사한를했지만에 속성을 할당하고 SliderValueProperty를 할당하면 내 문제를 해결하면서 속성 변경 이벤트가 발생합니다. 한 가지 질문 - 개인용 백킹 필드에서 계산을 수행 한 다음 속성 변경 이벤트를 수동으로 발생시키는 이유는 무엇입니까? – xdumaine
문제에 대한 설명이 주어지면 MVVM을 사용할 때 가장 명확하고 직관적 인 방법으로 보였습니다.다른 부동산에 대한 조건을 변경 한 부동산 개발가가 있었고 문제없이 선택한 방식대로 할 수는 있었지만 프로그램에 대해 다른 것을 알지 못했습니다. 일반적으로 변경 사항을 UI에 표시하려면 PropertyChanged를 발생시키는 것에 대해 명시 적으로 선택합니다. –
예. 그것은 if (SliderValueProperty> MaxValueProperty) {SliderValueProperty = MaxValueProperty; }'및'if (_SliderValueProperty> _MaxValueProperty) {_ SliderValueProperty = _MaxValueProperty; RaisePropertyChanged (SliderValueProperty);}' – xdumaine