[英]DataTemplate bound items not changing with bound content
I have a set of tabs I'm displaying in an Avalonia TabControl
, each represented by a TabModel
object.我在 Avalonia
TabControl
中显示了一组选项卡,每个选项卡都由一个TabModel
对象表示。
TabModel.cs
: TabModel.cs
:
public class TabModel
{
public string Name { get; set; }
public string Content { get; set; }
}
I want each tab to have a TextBlock
as its title (similar to the example from the docs ) and a custom control as the body.我希望每个选项卡都有一个
TextBlock
作为其标题(类似于docs 中的示例)和一个自定义控件作为正文。 When loading the TabControl
, the titles and body display properly.加载
TabControl
时,标题和正文显示正确。 However, when I modify the Name
or Content
fields, the controls won't update.但是,当我修改
Name
或Content
字段时,控件不会更新。 When Content
is changed by the control (through the TwoWay
binding), the change is reflected in the field and any changes made by the code are forgotten.当控件(通过
TwoWay
绑定)更改Content
时,更改会反映在字段中,并且代码所做的任何更改都将被遗忘。
Xaml: xml:
<TabControl Items="{Binding Tabs}">
<TabControl.ItemTemplate>
<TreeDataTemplate DataType="models:TabModel">
<TextBlock Text="{Binding Name}" />
</TreeDataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<TreeDataTemplate DataType="models:TabModel">
<controls:BoundEditor
BoundText="{Binding Content, Mode=TwoWay}" Name="BoundEditor" />
</TreeDataTemplate>
</TabControl.ContentTemplate>
</TabControl>
At the moment, your model is a plain POCO.目前,您的模型是一个普通的 POCO。 P lain O ld C LR O bject.
普通老 C LR对象。
What you need is a ViewModel ...你需要的是一个 ViewModel ...
public class TabModel : ReactiveObject
{
private string _name;
public string Name
{
get { return _name; }
set { this.RaiseAndSetIfChanged(ref _name, vaue); }
}
...
}
Now every time the Name
setter is called, it will check to see if the value is new, update it if it is, and then will notify UI to update.现在每次调用
Name
setter 时,它都会检查值是否是新的,如果是则更新它,然后通知 UI 进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.