在我的Resources.xaml(资源字典)中,我有一个DataTemplate:
<DataTemplate x:Key="ProductDetailsContentTemplate">
<StackPanel Orientation="Vertical" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Button x:Name="SubtractQytyButton" Grid.Column="0" Content="-" FontSize="24" HorizontalAlignment="Stretch" Command="{Binding SubtractQuantityCommand}"
Margin="5,0" Style="{StaticResource ButtonStyle}" FontWeight="ExtraBold" />
<TextBox x:Name="QuantityTextBox" Grid.Column="1" HorizontalAlignment="Stretch" FontSize="24" Margin="5,0"
KeyDown="Quantity_Keydown" TextChanging="Quantity_TextChanging" TextChanged="QuantityTextBox_TextChanged"
Text="{Binding QuantityT, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MaxLength="5" TextAlignment="Center"/>
<Button x:Name="AddQytyButton" Grid.Column="2" Content="+" FontSize="24" HorizontalAlignment="Stretch" Command="{Binding AddQuantityCommand}" />
</Grid>
</StackPanel>
在我的Page1上,我将datatemplate称为contentcontrol
在我的page1VM上,有一个包含属性的继承类:
public class Class1 : BindableBase
{
private Model _model;
public Class1(Model model) {
_model = model;
_quantityT = model.Quantity.ToString();
SubtractQuantityCommand = new DelegateCommand(SubtractQuantity, CanSubtract);
AddQuantityCommand = new DelegateCommand(AddQuantity, CanAdd);
}
public DelegateCommand SubtractQuantityCommand { get; }
public DelegateCommand AddQuantityCommand { get; }
private bool CanAdd() {
return int.Parse(QuantityT) < 99999;
}
private void AddQuantity() {
QuantityT = (int.Parse(QuantityT) + 1).ToString();
}
private bool CanSubtract() {
return int.Parse(QuantityT) > 0;
}
private void SubtractQuantity() {
QuantityT = (int.Parse(QuantityT) - 1).ToString();
}
private void InvalidateCommands() {
(AddQuantityCommand as DelegateCommand)?.RaiseCanExecuteChanged();
(SubtractQuantityCommand as DelegateCommand)?.RaiseCanExecuteChanged();
}
private string _quantityT;
public string QuantityT {
get { return _quantityT; }
set { this.Set(ref _quantityT, value);
InvalidateCommands();
}
}
}
我的BindableBase:
public abstract class BindableBase : IBindable
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
return;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual bool Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (object.Equals(storage, value))
return false;
storage = value;
RaisePropertyChanged(propertyName);
return true;
}
}
我的问题是,例如QuantityT值为1,当我点击减号或加号按钮时,QuantityT属性设置为0或2,但它没有更新文本框值,它仍然显示1,即使我添加< / p>
UpdateSourceTrigger=PropertyChanged
谢谢,
NicoTing
答案 0 :(得分:0)
[CallerMemberName]string propertyName
赢回“&#34; QuantityT&#34;。它将返回&#34; SetQuantityT&#34;或&#34; GetQuantityT&#34; (现在不能测试它,但是那样的东西)。您必须从propertyName中删除前3个字符。 (我记得以前遇到类似的问题)
而不是
public abstract class BindableBase : IBindable
使用
public abstract class BindableBase : IBindable, INotifyPropertyChanged
希望有所帮助
答案 1 :(得分:0)
我测试了您的代码段,添加了继承INotifyPropertyChanged
,如下所示,它可以正常运行。
public abstract class BindableBase : INotifyPropertyChanged
由于您的代码段不完整,我上传了一个完整的,最小的项目here,您可以进行测试并比较错误。如果您仍有问题,请上传您的最小重现项目。
结果在我身边:
答案 2 :(得分:0)
我认为问题是因为datatemplate在ContentDialoag中,所以它没有及时更新值,因为当我关闭并重新打开contentdialog时,值会被更改,所以我所做的就是修复后面的代码
感谢您的回复