【问题标题】:Is it possible to open Expander from ViewModel?是否可以从 ViewModel 打开 Expander?
【发布时间】:2021-05-02 04:41:00
【问题描述】:

我的视图中有一个Expander,每次触摸它都会打开并显示数据。是否可以打开 Expander 或从 ViewModel 中关闭它?我想做的是Expander可以通过按Button来打开或关闭它。

MainPage.xaml:

    <StackLayout>
        <StackLayout>
            <CollectionView>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <ContentView>
                            <Frame>
                                <StackLayout>
                                    <Expander x:Name="expander"></Expander>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
        <StackLayout>
            <ImageButton Source="img1" Command="{Binding xxxCommand}" CommandParameter="{Binding Source={x:Reference expander}, Path=.}"/>
        </StackLayout>
    </StackLayout>

视图模型:

    xxxCommand = new Command((sender)=> {

     var exp = sender as Expander;
     exp.IsExpanded = !exp.IsExpanded;
     //...
            
});

当我打开应用程序时,我得到了这个异常:

Xamarin.Forms.Xaml.XamlParseException:找不到对象 被扩展器引用。

【问题讨论】:

    标签: c# xamarin.forms xamarin-community-toolkit


    【解决方案1】:

    您可以将 Expander 作为参数传递给按钮的 Command。

    设置扩展器的名称

     <Expander x:Name="exp">
    
     <Button Text="XXX"  Command="{Binding xxxCommand}" CommandParameter="{Binding Source={x:Reference exp}, Path=.}" />
    

    在视图模型中

    xxxCommand = new Command((sender)=> {
    
         var exp = sender as Expander;
         exp.IsExpanded = !exp.IsExpanded;
         //...
                
    });
    

    更新

    在您的情况下,您可以使用数据绑定

    <StackLayout>
          <Expander  IsExpanded="{Binding IsExpanded}"></Expander>
    </StackLayout>
    
    <ImageButton Source="img1" Command="{Binding xxxCommand}"/>
    

    在模型中添加新属性

    public class YourModel : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string propertyname)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
    
        public bool isExpanded;
        public bool IsExpanded
        {
            get { return isExpanded; }
            set
            {
                isExpanded= value;
                OnPropertyChanged("IsExpanded");
            }
        }
    
    
        //...other properties
    
    }
    

    在视图模型中

    //list here is the ItemsSource of your listview
    ObservableCollection<YourModel> list = new ObservableCollection<YourModel>();
    
    //list.Add();
    //list.Add();
    //list.Add();
    //list.Add();
    
    Command xxxCommand = new Command(()=> {
    
       //if you want to open the first Expander  
    
       list[0].IsExpanded = true;
    
    
    });
    

    【讨论】:

    • 我对此有疑问。我的应用程序崩溃了,因为我的 Expander 在 CollectionView、Frame 和 StackLayout 内,当将其 x: Name 放入 CommandParameter 时,它告诉我它没有找到任何名为 expander 的东西。
    • 已知 Expander 控件在 ListView 或 CollectionView 中使用时会显示不需要的行为。目前我们建议不要在这些控件之一中使用扩展器。检查docs.microsoft.com/en-us/xamarin/community-toolkit/views/…
    • 检查我的更新答案。如果它对你有帮助,别忘了接受它:)
    • 不能使用列表[0].IsExpanded = true; , 我只能使用 IsExpanded = true;单独它不起作用,它被调用但视图中没有任何变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    相关资源
    最近更新 更多