WinRT滑块值不会更新可观察的集合值

时间:2015-12-13 12:38:20

标签: c# windows-runtime slider winrt-xaml observablecollection

我在WinRT metro app中使用滑块。我的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)
    {

    }

我在更改事件中放置了断点但没有发生任何事情。这是屏幕截图 enter image description here

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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

  

在添加,删除,更改,移动项目或刷新整个列表时发生。

将断点置于QuantityInstalled的设置器中,您将看到值正在发生变化。

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