C# WinRT滑块值不更新可观察集合值
我正在WinRT metro应用程序中使用slider。我的ViewModel Quantity和QuantityInstalled中有两个属性。滑块最小值设置为0,最大值绑定到数量,值绑定到安装的数量。基本上,我想记录下哪个产品的QuantityInstalled值发生了变化,然后最终更新我的数据库。 这是我的看法模型C# WinRT滑块值不更新可观察集合值,c#,windows-runtime,slider,winrt-xaml,observablecollection,C#,Windows Runtime,Slider,Winrt Xaml,Observablecollection,我正在WinRT metro应用程序中使用slider。我的ViewModel Quantity和QuantityInstalled中有两个属性。滑块最小值设置为0,最大值绑定到数量,值绑定到安装的数量。基本上,我想记录下哪个产品的QuantityInstalled值发生了变化,然后最终更新我的数据库。 这是我的看法模型 public class SurveyProductDetails : INotifyPropertyChanged { private Guid _RoomId;
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代码
observeCollection surveydeail=新的observeCollection();
surveyDetail.CollectionChanged+=surveyDetail\u CollectionChanged;
void surveyDetail\u CollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
}
我已经在changes事件中设置了断点,但什么也没发生。这是屏幕截图
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代码中将绑定模式设置为双向。将“Mode=TwoWay
”添加到{Binding…}
事件CollectionChanged
在您更改模型属性时不会触发。这是正常的行为
在添加、删除、更改、移动项或刷新整个列表时发生
将断点放在QuantityInstalled
的setter中,您将看到值正在更改
您可以将处理程序添加到滑块的
ValueChanged
事件中。但另一方面,我不认为每次将值更改为1时都保存数据是个好主意。没有发生任何事情,我更改了此值=“{Binding QuantityInstalled,Mode=TwoWay}”
<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>