我在做一个简单的图书编辑。
在图书编辑窗口中,我希望主题和子主题按层次顺序显示,因此我制作了一个带有复杂数据模板的ItemsControl来显示它们。我现在拥有的是:

主题是按钮,如果单击相应的主题按钮(例如,如果是下拉列表),则子主题列表将是可见的。
问题是我使用的是MVVM,我不能只在IsVisible事件中更改ListBox的OnClick属性。
我如何做到这一点而不打破和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"));
}
}
} 发布于 2021-06-11 05:37:14
我怀疑您正在询问有关WPF数据绑定的内容。我是Xamarin.Forms开发人员,如果我为Xamarin.Forms做这件事的话,我可以告诉你我会怎么做。
首先,更新“
,如果使用的是ReactiveUI,则可以从ReactiveUI继承
<Button Name="kekw" Content="{Binding Name}" />绑定到该命令以更新布尔属性值。( CommandParameter绑定到主题。在Xamarin.Forms中是“.”;https://stackoverflow.com/questions/67930609
复制相似问题