[英]UWP Xaml Textblock Data Binding - UI not updating even though the property is updated
首先是免责声明:我已经阅读了数十篇文章和数百个答案,但我还没有找到解决方案。 我已尝试通过我能找到的所有方式解决此问题,但 UI 并未更新。
我正在编写一个 UWP 应用程序作为个人项目。 在这个特定的场景中,我有一个网格,其中分配了 DataContext,其中包含一系列绑定到属性的 TextBlock。 我使用的是“Binding”而不是“x:Bind”,因为网格是 ControlTemplate 的一部分。
问题:UI 完全忽略更改并且从不更新。
我已经在页面构造函数中设置了一个值的属性,并且已经设置,但是此后没有应用任何更改......永远。
请查看下面的相关代码片段,也许有人可以看到我没有看到的内容。
提前致谢。
网格 XAML:
<Grid
Grid.Row="1"
BorderBrush="{StaticResource SystemControlChromeLowAcrylicElementBrush}"
BorderThickness="2,0,2,2">
<Grid.DataContext>
<views:MenusPage />
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="152" />
<ColumnDefinition Width="152" />
<ColumnDefinition Width="152" />
<ColumnDefinition Width="152" />
<ColumnDefinition Width="152" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="3"
Padding="15,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontWeight="Bold"
Text="Totals" />
<Border
Grid.Row="0"
Grid.Column="3"
Margin="0,2,0,-2"
BorderBrush="{StaticResource SystemControlChromeLowAcrylicElementBrush}"
BorderThickness="2,0,0,0">
<TextBlock
Padding="10,0,0,0"
VerticalAlignment="Center"
FontWeight="Bold"
Text="{Binding CaloriesTotal, Mode=OneWay}" />
</Border>
<Border
Grid.Row="0"
Grid.Column="4"
Margin="0,2,0,-2"
BorderBrush="{StaticResource SystemControlChromeLowAcrylicElementBrush}"
BorderThickness="2,0,0,0">
<TextBlock
Padding="10,0,0,0"
VerticalAlignment="Center"
FontWeight="Bold"
Text="{Binding ProteinTotal}" />
</Border>
<Border
Grid.Row="0"
Grid.Column="5"
Margin="0,2,0,-2"
BorderBrush="{StaticResource SystemControlChromeLowAcrylicElementBrush}"
BorderThickness="2,0,0,0">
<TextBlock
Padding="10,0,0,0"
VerticalAlignment="Center"
FontWeight="Bold"
Text="{Binding FatTotal}" />
</Border>
<Border
Grid.Row="0"
Grid.Column="6"
Margin="0,2,0,-2"
BorderBrush="{StaticResource SystemControlChromeLowAcrylicElementBrush}"
BorderThickness="2,0,0,0">
<TextBlock
Padding="10,0,0,0"
VerticalAlignment="Center"
FontWeight="Bold"
Text="{Binding CarbTotal}" />
</Border>
<Border
Grid.Row="0"
Grid.Column="7"
Margin="0,2,0,-2"
BorderBrush="{StaticResource SystemControlChromeLowAcrylicElementBrush}"
BorderThickness="2,0,0,0">
<TextBlock
Padding="15,0,0,0"
VerticalAlignment="Center"
FontWeight="Bold"
Text="{Binding SugarTotal}" />
</Border>
</Grid>
特性:
private double _caloriesTotal;
public double CaloriesTotal
{
get { return _caloriesTotal; }
set { Set(ref _caloriesTotal, value); }
}
private double _proteinTotal;
public double ProteinTotal
{
get { return _proteinTotal; }
set { Set(ref _proteinTotal, value); }
}
private double _fatTotal;
public double FatTotal
{
get { return _fatTotal; }
set { Set(ref _fatTotal, value); }
}
private double _carbTotal;
public double CarbTotal
{
get { return _carbTotal; }
set { Set(ref _carbTotal, value); }
}
private double _sugarTotal;
public double SugarTotal
{
get { return _sugarTotal; }
set { Set(ref _sugarTotal, value); }
}
属性已更改:
public event PropertyChangedEventHandler PropertyChanged;
private void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(storage, value))
{
return;
}
storage = value;
OnPropertyChanged(propertyName);
}
private void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
代码设置值:(注意:这是缩短的版本,但效果相同,在构造函数中设置值会应用值和 UI 更新,但在页面上的任何其他位置设置值,在执行的任何方法中,它们是被忽略并且 UI 显示为零。)
CaloriesTotal = 10;
ProteinTotal = 20;
FatTotal = 30;
CarbTotal = 40;
SugarTotal = 50;
我找到了解决方案,虽然我对它的实际工作方式感到目瞪口呆,而其他应该工作的“正常”解决方案却没有,但超出了我的范围。
无论如何,感谢@jsmyth886 和@Alexey 在故障排除方面提供的帮助和建议。
自发布这个问题以来,我尝试将属性分离到另一个类中,在<Page.Resources>
块中引用<Models:MenuTypesModel x:Key="MenuTotals" />
- 失败
我尝试设置Page
本身的DataContext
- 失败
如前所述,我的问题中的<Grid>... </Grid>
XAML 是ControlTemplate
的一部分不同的命名结构和路径,所以最终不应该发生冲突——这也不起作用,所以——失败了
@Alexey 的回应让我换了个思路,我的搜索最终让我找到了这篇文章Binding to Self/'this' in XAML ,它解决了我的问题。
我从网格中删除了以下数据上下文:
<Grid.DataContext>
<views:MenusPage />
</Grid.DataContext>
并将其替换为此DataContext="{Binding ElementName=_this}"
并确保我的文本块绑定,最终结果(非常短的版本):
<Grid ... DataContext="{Binding ElementName=_this}">
...
<TextBlock ... Text="{Binding CaloriesTotal}" />
...
</Grid>
最后将 Name 属性添加到 Page 本身:
x:Name="_this"
它奏效了!!!!
我设置属性的方式没有任何改变。
我的属性仍然根据问题定义,我没有视图模型或其他类,也没有在代码隐藏中设置 DataContext。
只需将DataContext="{Binding ElementName=_this}"
网格和x:Name="_this"
添加到页面,即可让 UI 每次都反映对属性的更改。
一个奇怪的简单解决方案。
注意:虽然,虽然我在尝试不同的事情,但当我在 ContentTemplate 之外的页面本身上有网格时,我在代码隐藏中设置了页面的DataContext = this;
它开始填充但仍然未能影响控件模板中的网格。
因此,除非存在我没有看到的冲突,或者因为我有其他不相关的数据源以及直接分配给需要它的控件......我无法理解为什么它现在可以使用这个简单的绑定。
无论如何,感谢您的帮助,我希望这对将来的其他人有所帮助。
请为您的主要对象使用绑定 DataContext,而不是 DataGrid:
<Window.DataContext>
<views:MenusPage />
</Window.DataContext>
如果您想从 NavigationCacheMode (Enabled/Required) 中受益以提高性能,则必须在数据上下文更改时更新绑定。
public MainPage()
{
this.InitializeComponent();
this.DataContextChanged += (s, e) => this.Bindings.Update();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.