2011-03-19 2 views
1

이 모든, 잘 작동에

  • MaxValueProperty

  • 바인드 slider1.Maximum

  • slider1.Value에 바인딩 계산하고, MaxValueProperty한다 가 계산되어 SliderValueProperty보다 작습니다. 보기에서

    은 : MaxValueProperty가 변경 되었기 때문에

    • slider1.Maximum가 업데이트됩니다
    • slider1.Value는 슬라이더의 기본 동작으로 slider1.Maximum로 설정되어있을 때 값보다 작은 경우 최대 변경됩니다.

    그러나,이 경우, SliderValueProperty 새로운 slider1.Value

    <Slider Name="slider1" Maximum="{Binding Path=MaxValueProperty, Mode=TwoWay}" Value="{Binding Path=SliderValueProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
    

    내가 바인딩 레이블을 가지고 있기 때문에 나는 slider1.Value이 변경지고 있음을 알고 있으며, 라벨에 업데이트되지 않습니다 변경

    <Label Content="{Binding ElementName=slider, Path=Value, Converter={StaticResource NumberToStringConverter}}" /> 
    

    바인딩이 어떻게 업데이트 될 수 있습니까?

  • 답변

    0

    오히려 슬라이더의 기본 동작과 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> 
    
    +0

    을, 나는 문 만약 당신 유사한를했지만에 속성을 할당하고 SliderValueProperty를 할당하면 내 문제를 해결하면서 속성 변경 이벤트가 발생합니다. 한 가지 질문 - 개인용 백킹 필드에서 계산을 수행 한 다음 속성 변경 이벤트를 수동으로 발생시키는 이유는 무엇입니까? – xdumaine

    +0

    문제에 대한 설명이 주어지면 MVVM을 사용할 때 가장 명확하고 직관적 인 방법으로 보였습니다.다른 부동산에 대한 조건을 변경 한 부동산 개발가가 있었고 문제없이 선택한 방식대로 할 수는 있었지만 프로그램에 대해 다른 것을 알지 못했습니다. 일반적으로 변경 사항을 UI에 표시하려면 PropertyChanged를 발생시키는 것에 대해 명시 적으로 선택합니다. –

    +0

    예. 그것은 if (SliderValueProperty> MaxValueProperty) {SliderValueProperty = MaxValueProperty; }'및'if (_SliderValueProperty> _MaxValueProperty) {_ SliderValueProperty = _MaxValueProperty; RaisePropertyChanged (SliderValueProperty);}' – xdumaine

    관련 문제