列表框绑定 (Caliburn.Micro) - 疑难解答
Listbox Binding (Caliburn.Micro) - troubleshoot
我正在使用 Caliburn.Micro 并尝试使用 MVVM;所以我的 ViewModel 和 Views 是分开的;
我已经从 View 和 运行 中删除了 xaml.cs 到一个问题,我的 ListBox 没有绑定到 public ObservableCollection(我无法弄清楚如何)
考虑以下 XAML:
<Controls:Tile Title="Track & Trace Reset"
Controls:ControlsHelper.MouseOverBorderBrush="{DynamicResource MahApps.Brushes.ThemeForeground}"
Width="155" Height="155"
HorizontalTitleAlignment="Center"
x:Name="Button" />
<ListBox x:Name="LogEntries" ItemsSource="{Binding LogEntries}"/>
和以下 class:
public class DebugViewModel
{
public ObservableCollection<LogEntryModel> LogEntries = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
public void Button()
{
GetLogEntries();
}
private void GetLogEntries() {
LogEntries = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
//if filter exists, filter the list
return;
//Format based on Severity
}
}
"Button"-绑定按预期工作;但是 ListBox 不显示任何内容(我至少期望它应该显示模型中的一些原始文本)。
LogEntries 被填充(8 个条目)- 这也起作用;
如何解决绑定问题?
LogEntries
应定义为 public 属性 才能使绑定生效:
public ObservableCollection<LogEntryModel> LogEntries { get; }
= new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
另请注意,使用 Caliburn.Micro,按照惯例,您只需在视图中设置 Name
属性 即可绑定到相应视图模型中的 属性 :
<ListBox x:Name="LogEntries" />
我正在使用 Caliburn.Micro 并尝试使用 MVVM;所以我的 ViewModel 和 Views 是分开的; 我已经从 View 和 运行 中删除了 xaml.cs 到一个问题,我的 ListBox 没有绑定到 public ObservableCollection(我无法弄清楚如何)
考虑以下 XAML:
<Controls:Tile Title="Track & Trace Reset"
Controls:ControlsHelper.MouseOverBorderBrush="{DynamicResource MahApps.Brushes.ThemeForeground}"
Width="155" Height="155"
HorizontalTitleAlignment="Center"
x:Name="Button" />
<ListBox x:Name="LogEntries" ItemsSource="{Binding LogEntries}"/>
和以下 class:
public class DebugViewModel
{
public ObservableCollection<LogEntryModel> LogEntries = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
public void Button()
{
GetLogEntries();
}
private void GetLogEntries() {
LogEntries = new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
//if filter exists, filter the list
return;
//Format based on Severity
}
}
"Button"-绑定按预期工作;但是 ListBox 不显示任何内容(我至少期望它应该显示模型中的一些原始文本)。 LogEntries 被填充(8 个条目)- 这也起作用;
如何解决绑定问题?
LogEntries
应定义为 public 属性 才能使绑定生效:
public ObservableCollection<LogEntryModel> LogEntries { get; }
= new ObservableCollection<LogEntryModel>(GlobalConfig.Connection.GetLogEntries());
另请注意,使用 Caliburn.Micro,按照惯例,您只需在视图中设置 Name
属性 即可绑定到相应视图模型中的 属性 :
<ListBox x:Name="LogEntries" />