【问题标题】:How can I change IsVisible property of my ListBox?如何更改 ListBox 的 IsVisible 属性?
【发布时间】: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


    【解决方案1】:

    我怀疑您在询问有关 WPF 数据绑定的问题。 我是 Xamarin.Forms 开发人员,如果我要为 Xamarin.Forms 做,我可以建议我会做什么。

    1. 首先,更新“主题”类,使其具有一个布尔属性来控制子主题是显示还是隐藏。 (确保类实现了INotifyPropertyChanged,如果你使用的是ReactiveUI,你可以继承自ReactiveObject);

    2. 在您的 VM 中创建一个命令,并将 &lt;Button Name="kekw" Content="{Binding Name}" /&gt; 绑定到该命令以更新布尔属性值。 (并且 CommandParameter 绑定到主题。在 Xamarin.Forms 中它是“.”);

    3. 将 ListBox.IsVisible 绑定到该布尔属性。

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 2019-12-14
      相关资源
      最近更新 更多