Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
滑块上的WPF绑定不会在最大更改时更新 combobox1.SelectedItem绑定到SelectedItemProperty 设置SelectedItemProperty时,将计算MaxValueProperty_Wpf_Data Binding_Mvvm - Fatal编程技术网

滑块上的WPF绑定不会在最大更改时更新 combobox1.SelectedItem绑定到SelectedItemProperty 设置SelectedItemProperty时,将计算MaxValueProperty

滑块上的WPF绑定不会在最大更改时更新 combobox1.SelectedItem绑定到SelectedItemProperty 设置SelectedItemProperty时,将计算MaxValueProperty,wpf,data-binding,mvvm,Wpf,Data Binding,Mvvm,MaxValueProperty绑定到slider1。最大值 slider1.Value绑定到SliderValueProperty 除了当combobox1.SelectedItem发生更改,并且计算了MaxValueProperty,结果小于SliderValueProperty时,这些都可以正常工作 他认为: slider1.Maximum已更新,因为MaxValueProperty已更改 slider1.Value设置为slider1.max,作为最大值更改为小于该值时滑块的默认行为

MaxValueProperty
绑定到
slider1。最大值

  • slider1.Value
    绑定到
    SliderValueProperty

  • 除了当
    combobox1.SelectedItem
    发生更改,并且计算了
    MaxValueProperty
    ,结果小于
    SliderValueProperty
    时,这些都可以正常工作

    他认为:

    • slider1.Maximum
      已更新,因为
      MaxValueProperty
      已更改
    • slider1.Value
      设置为
      slider1.max
      ,作为最大值更改为小于该值时滑块的默认行为
    但是,发生这种情况时,
    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}}" />
    
    
    

    如何确保绑定已更新?

    与其依赖滑块的默认行为和双向
    绑定,不如在
    SelectedItemProperty
    setter中计算新的最大值后,首先使用新的最大值在“SliderValueProperty”上,然后在“MaxValueProperty”。在私人支持字段上进行计算,而不是在公共属性上

    这将导致系统首先调整滑块的值,然后调整maxvalue,并且它将为您移动滑块位置

    将slider1的
    最大值
    上的绑定模式也设置为
    单向
    ;我认为这样就可以了

    对于这样的viewmodel,如下所示:

    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
    
        <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>
    
    XAML看起来有点像这样:

    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
    
        <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>
    
    
    
    在您回答之前不久,我对您的if语句做了类似的处理,但是是在属性上,分配SliderValueProperty会引发propertychanged事件,解决了我的问题。但有一个问题-为什么要在私有支持字段上进行计算,然后手动引发property changed事件?根据您的问题描述,请回答当MVVM使用时,这似乎是最明确、最直接的方法。您有一个属性设置器,它可以更改其他属性的条件,虽然您可以按照您选择的方式毫无问题地执行,但我对您的程序一无所知。我通常选择在需要更改时明确提出PropertyChanged是的,这只是
    if(SliderValueProperty>MaxValueProperty){SliderValueProperty=MaxValueProperty;}
    if(SliderValueProperty>\MaxValueProperty){SliderValueProperty=\MaxValueProperty;RaisePropertyChanged(SliderValueProperty);}