WinRT 滑块值不更新 observablecollection 值

WinRT slider value doesnot update observable collection value

我在我的 WinRT metro 中使用滑块 app.I 我的 ViewModel Quantity 和 QuantityInstalled 中有两个属性。 Slider Min 值设置为 0,Max 绑定到 Quantity,Value 绑定到 QuantityInstalled。基本上我想记录哪个产品 QuantityInstalled 值发生变化,然后最后更新我的数据库。 这是我的视图模型。

  public class SurveyProductDetails : INotifyPropertyChanged
{
    private Guid _RoomId;
    private int _ProductTypeId;
    private string _Title;
    private string _Color;
    private int _Quantity;
    private int _QuantityInstalled;
    private string _RoomDescription;
    private string _ProductDescription;
    private string _ProductSpecification;
    public Guid RoomId
    {
        get { return _RoomId; }

        set
        {
            _RoomId = value;
            NotifyPropertyChanged("RoomId");
        }
    }
    public int ProductTypeId
    {
        get { return _ProductTypeId; }
        set
        {
            _ProductTypeId = value;
            NotifyPropertyChanged("ProductTypeId");
        }
    }
    public string Title
    {
        get { return _Title; }
        set
        {
            _Title = value;
            NotifyPropertyChanged("Title");
        }
    }
    public string Color
    {
        get { return _Color; }
        set
        {
            _Color = value;
            NotifyPropertyChanged("Color");
        }
    }
    public string ProductSpecification
    {
        get { return _ProductSpecification; }

        set
        {
            _ProductSpecification = value;
            NotifyPropertyChanged("ProductSpecification");
        }
    }
    public string ProductDescription
    {
        get { return _ProductDescription; }

        set
        {
            _ProductDescription = value;
            NotifyPropertyChanged("ProductDescription");
        }
    }
    public int Quantity
    {
        get { return _Quantity; }

        set
        {
            _Quantity = value;
            NotifyPropertyChanged("Quantity");
        }
    }
    public int QuantityInstalled
    {
        get { return _QuantityInstalled; }

        set
        {
            _QuantityInstalled = value;
            NotifyPropertyChanged("QuantityInstalled");
        }
    }
    public string RoomDescription
    {
        get { return _RoomDescription; }

        set
        {
            _RoomDescription = value;
            NotifyPropertyChanged("RoomDescription");
        }
    }
   public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 
}

这是我的 C# 代码

  ObservableCollection<SurveyProductDetails> surveyDetail = new ObservableCollection<SurveyProductDetails>();

    surveyDetail.CollectionChanged += surveyDetail_CollectionChanged;
     void surveyDetail_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {

    }

我在更改事件中设置了断点,但没有任何反应。这是屏幕截图

Xaml代码:

  <ListView Grid.Row="1" ItemsSource="{Binding}" BorderBrush="Black" BorderThickness="1,1,1,3" SelectionMode="None">
                                        <ListView.ItemContainerStyle>
                                            <Style TargetType="ListViewItem">
                                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                                <Setter Property="Margin" Value="-3,-5,-3,-10"/>
                                                <Setter Property="Padding" Value="0"></Setter>
                                            </Style>

                                        </ListView.ItemContainerStyle>
                                        <ListView.ItemTemplate>
                                            <DataTemplate>
                                                <Grid Grid.Row="1"  HorizontalAlignment="Stretch" Background="#e5e6e6">
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto"/>
                                                    </Grid.RowDefinitions>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*"/>
                                                        <ColumnDefinition Width="100"/>
                                                        <ColumnDefinition Width="1.5*"/>
                                                        <ColumnDefinition Width="1.5*"/>
                                                        <ColumnDefinition Width="1.4*"/>
                                                    </Grid.ColumnDefinitions>


                                                    <Border Grid.Row="1" BorderBrush="#000" BorderThickness="02,02,0,0">
                                                        <TextBlock Text="{Binding RoomDescription}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25" FontWeight="SemiBold" Foreground="#000"></TextBlock>
                                                    </Border>
                                                    <Border Grid.Row="1" Grid.Column="1" Background="{Binding Color}" BorderBrush="#000" BorderThickness="02,02,0,0">
                                                        <TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" FontWeight="Black" Foreground="#000"></TextBlock>
                                                    </Border>
                                                    <Border Grid.Row="1" Grid.Column="2" BorderBrush="#000" BorderThickness="02,02,0,0">
                                                        <TextBlock TextWrapping="Wrap" Margin="10,0,0,0" Text="{Binding ProductSpecification}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="20" FontWeight="SemiBold"  Foreground="#000"></TextBlock>
                                                    </Border>
                                                    <Border Grid.Row="1" Grid.Column="3" BorderBrush="#000" BorderThickness="02,02,02,0">
                                                        <TextBlock TextWrapping="Wrap"  Margin="10,0,0,0" Text="{Binding ProductDescription}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="20" FontWeight="SemiBold"  Foreground="#000"></TextBlock>
                                                    </Border>
                                                    <Border Grid.Row="1" Grid.Column="4" BorderBrush="#000" BorderThickness="0,02,02,0">
                                                        <Slider x:Name="AddQuantitySlider" ValueChanged="AddQuantitySlider_ValueChanged" Orientation="Horizontal" Background="#808285" Width="300" HorizontalContentAlignment="Stretch" HorizontalAlignment="Center" VerticalAlignment="Center" Maximum="{Binding Quantity}" Minimum="0" Value="{Binding  QuantityInstalled ,Mode=TwoWay}">
                                                            <Slider.HeaderTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel>
                                                                        <TextBlock FontWeight="SemiBold"  Foreground="#000" >
                                                                            <Run Text="{Binding ElementName=AddQuantitySlider, Mode=OneWay, Path=Value}"></Run>
                                                                            <Run>of</Run>
                                                                            <Run Text="{Binding Quantity}"></Run>
                                                                        </TextBlock>
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </Slider.HeaderTemplate>
                                                        </Slider>
                                                     </Border>
                                                </Grid>

                                            </DataTemplate>
                                        </ListView.ItemTemplate>


                                    </ListView>

您需要在 XAML 代码中将绑定模式设置为 TwoWay。将“Mode=TwoWay”添加到您的 {Binding...}

当您更改 属性 模型时,事件 CollectionChanged 不会触发。这是正常行为。 https://msdn.microsoft.com/library/ms653375(v=vs.110).aspx

Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.

QuantityInstalled 的 setter 中放置断点,您将看到值在变化。

您可以将处理程序添加到滑块的 ValueChanged 事件。但另一方面,我不认为每次将值更改 1 时都保存数据是个好主意。