【发布时间】:2021-06-11 02:37:05
【问题描述】:
我正在制作一个简单的图书编辑器。
在图书编辑窗口中,我希望主题和子主题以分层顺序显示,因此我制作了一个带有复杂数据模板的 ItemsControl 来显示它们。我现在拥有的是:
主题是按钮,如果单击相应的主题按钮(就像它是下拉列表一样),我希望子主题列表框可见。
问题是我正在使用 MVVM,我不能只在 OnClick 事件中更改 ListBox 的 IsVisible 属性。
如何在不破坏和 MVVM 模式的情况下做到这一点?
这是我的 XAML 代码:
<ScrollViewer>
<Grid ColumnDefinitions="auto, *" RowDefinitions="auto">
<ItemsControl Grid.Column="0" Grid.Row="0" Items="{Binding Book.Themes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Name="kekw" Content="{Binding Name}" />
<ListBox Items="{Binding Subthemes}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</ScrollViewer>
这是视图模型:
public class EditBookViewModel
{
private Book book;
public Book Book
{
get
{
return book;
}
set
{
book = value;
}
}
public EditBookViewModel(string name, string path)
{
book = new Book(name, path);
book.Themes = new LinkedList<Theme>();
book.Themes.AddLast(new Theme("Theme 1", "Page 1"));
book.Themes.AddLast(new Theme("Theme 2", "Page 2"));
book.Themes.AddLast(new Theme("Theme 3", "Page 3"));
foreach(Theme t in book.Themes)
{
t.Subthemes = new LinkedList<Subtheme>();
t.Subthemes.AddLast(new Subtheme("Subtheme 1", "Page 1"));
t.Subthemes.AddLast(new Subtheme("Subtheme 2", "Page 2"));
t.Subthemes.AddLast(new Subtheme("Subtheme 3", "Page 3"));
}
}
}
【问题讨论】:
标签: c# reactiveui avaloniaui