C# UWP按钮从ListViewItem单击

C# UWP按钮从ListViewItem单击,c#,xaml,uwp,user-controls,C#,Xaml,Uwp,User Controls,我真的希望有经验的人能给我一些建议 我对UWP项目有以下设置: 在我的应用程序页面内的XAML中声明的ListView,Tubes.XAML: <ListView Name="TubesGrid" ItemsSource="{x:Bind TubeItems, Mode=TwoWay}" ItemTemplateSelector="{StaticResource TubeTemplateSelector}"

我真的希望有经验的人能给我一些建议

我对UWP项目有以下设置:

  • 在我的应用程序页面内的XAML中声明的
    ListView
    Tubes.XAML

    <ListView Name="TubesGrid"
                  ItemsSource="{x:Bind TubeItems, Mode=TwoWay}"
                  ItemTemplateSelector="{StaticResource TubeTemplateSelector}"
                  IsItemClickEnabled="True"
                  ItemClick="TubesGrid_ItemClick"
                  SelectionChanged="TubesGrid_SelectionChanged">
    
  • TubeTemplate
    中,我有一个按钮,在其他视图旁边:

    <Button Name="RemoveTube"
            Click="RemoveTube_Click"
        <Image
                Source="../Assets/xIcon.png"
                Stretch="None">
        </Image>
    </Button>
    

    如果看起来像是在不使用viewmodels的情况下执行此操作,则可以向TubeTemplate控件添加事件

     public event EventHandler Closed;
    
    单击关闭按钮时,您将触发事件

    private void RemoveTube_Click(object sender, RoutedEventArgs e)
    {
        Closed?.Invoke(this, EventArgs.Empty); // Even better would be to give the item clicked (the data context)
    }
    
    <local:TubeTemplate HorizontalAlignment="Stretch" 
                        VerticalAlignment="Stretch" 
                        Closed="TubeTemplate_Closed">
    
    </local:TubeTemplate>
    
    然后,您可以在主页上订阅该活动

    private void RemoveTube_Click(object sender, RoutedEventArgs e)
    {
        Closed?.Invoke(this, EventArgs.Empty); // Even better would be to give the item clicked (the data context)
    }
    
    <local:TubeTemplate HorizontalAlignment="Stretch" 
                        VerticalAlignment="Stretch" 
                        Closed="TubeTemplate_Closed">
    
    </local:TubeTemplate>
    

    如果您在不使用viewmodels的情况下执行此操作,则可以向TubeTemplate控件添加事件

     public event EventHandler Closed;
    
    单击关闭按钮时,您将触发事件

    private void RemoveTube_Click(object sender, RoutedEventArgs e)
    {
        Closed?.Invoke(this, EventArgs.Empty); // Even better would be to give the item clicked (the data context)
    }
    
    <local:TubeTemplate HorizontalAlignment="Stretch" 
                        VerticalAlignment="Stretch" 
                        Closed="TubeTemplate_Closed">
    
    </local:TubeTemplate>
    
    然后,您可以在主页上订阅该活动

    private void RemoveTube_Click(object sender, RoutedEventArgs e)
    {
        Closed?.Invoke(this, EventArgs.Empty); // Even better would be to give the item clicked (the data context)
    }
    
    <local:TubeTemplate HorizontalAlignment="Stretch" 
                        VerticalAlignment="Stretch" 
                        Closed="TubeTemplate_Closed">
    
    </local:TubeTemplate>
    
    想法是点击一个项目来选择它,但当我点击项目内的按钮时,我希望该项目被删除

    更好的方法是使用MainPage命令方法绑定button命令属性,并在代码中处理数据源。您可以参考以下代码

    代码隐藏

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            MakeDataSource();
            this.InitializeComponent();
            DataContext = this;
    
        }
        public ObservableCollection<string> Items { get; set; }
    
        private void MakeDataSource()
        {
            Items = new ObservableCollection<string>() { "Nico","CCor","Jack"};
    
        }
        public ICommand BtnCommand
        {
            get
            {
                return new CommadEventHandler<object>((s) => BtnClick(s));
            }
        }
    
        private void BtnClick(object s)
        {
            Items.Remove(s as string);
        }
    }
    public class CommadEventHandler<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;
    
        public Action<T> action;
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public void Execute(object parameter)
        {
            this.action((T)parameter);
        }
        public CommadEventHandler(Action<T> action)
        {
            this.action = action;
    
        }
    }
    
    公共密封部分类主页面:第页
    {
    公共主页()
    {
    MakeDataSource();
    this.InitializeComponent();
    DataContext=this;
    }
    公共ObservableCollection项{get;set;}
    私有void MakeDataSource()
    {
    Items=newobserveCollection(){“Nico”、“CCor”、“Jack”};
    }
    公共ICommand BtnCommand
    {
    得到
    {
    返回新的CommadEventHandler((s)=>BtnClick(s));
    }
    }
    私有void b单击(对象s)
    {
    项目。删除(作为字符串);
    }
    }
    公共类CommadEventHandler:ICommand
    {
    公共事件处理程序CanExecuteChanged;
    公共行动;
    公共布尔CanExecute(对象参数)
    {
    返回true;
    }
    public void Execute(对象参数)
    {
    这个动作((T)参数);
    }
    公共通讯员(行动)
    {
    这个动作=动作;
    }
    }
    
    Xaml代码

    请注意,我们需要将当前焦点listview项参数传递给命令方法,并将其从数据源中删除

    <Grid HorizontalAlignment="Stretch" x:Name="RootGrid">
        <ListView ItemsSource="{Binding Items}" x:Name="MyListView">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid  HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
                        <Button HorizontalAlignment="Right" 
                                Margin="0,0,30,0" 
                                Content="Favorite" 
                                Command="{Binding ElementName=MyListView,Path=DataContext.BtnCommand}" 
                                CommandParameter="{Binding}"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    
    
    
    想法是点击一个项目来选择它,但当我点击项目内的按钮时,我希望该项目被删除

    更好的方法是使用MainPage命令方法绑定button命令属性,并在代码中处理数据源。您可以参考以下代码

    代码隐藏

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            MakeDataSource();
            this.InitializeComponent();
            DataContext = this;
    
        }
        public ObservableCollection<string> Items { get; set; }
    
        private void MakeDataSource()
        {
            Items = new ObservableCollection<string>() { "Nico","CCor","Jack"};
    
        }
        public ICommand BtnCommand
        {
            get
            {
                return new CommadEventHandler<object>((s) => BtnClick(s));
            }
        }
    
        private void BtnClick(object s)
        {
            Items.Remove(s as string);
        }
    }
    public class CommadEventHandler<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;
    
        public Action<T> action;
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public void Execute(object parameter)
        {
            this.action((T)parameter);
        }
        public CommadEventHandler(Action<T> action)
        {
            this.action = action;
    
        }
    }
    
    公共密封部分类主页面:第页
    {
    公共主页()
    {
    MakeDataSource();
    this.InitializeComponent();
    DataContext=this;
    }
    公共ObservableCollection项{get;set;}
    私有void MakeDataSource()
    {
    Items=newobserveCollection(){“Nico”、“CCor”、“Jack”};
    }
    公共ICommand BtnCommand
    {
    得到
    {
    返回新的CommadEventHandler((s)=>BtnClick(s));
    }
    }
    私有void b单击(对象s)
    {
    项目。删除(作为字符串);
    }
    }
    公共类CommadEventHandler:ICommand
    {
    公共事件处理程序CanExecuteChanged;
    公共行动;
    公共布尔CanExecute(对象参数)
    {
    返回true;
    }
    public void Execute(对象参数)
    {
    这个动作((T)参数);
    }
    公共通讯员(行动)
    {
    这个动作=动作;
    }
    }
    
    Xaml代码

    请注意,我们需要将当前焦点listview项参数传递给命令方法,并将其从数据源中删除

    <Grid HorizontalAlignment="Stretch" x:Name="RootGrid">
        <ListView ItemsSource="{Binding Items}" x:Name="MyListView">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid  HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
                        <Button HorizontalAlignment="Right" 
                                Margin="0,0,30,0" 
                                Content="Favorite" 
                                Command="{Binding ElementName=MyListView,Path=DataContext.BtnCommand}" 
                                CommandParameter="{Binding}"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    
    
    
    谢谢您抽出时间。我现在就来试试这个解决方案。谢谢你抽出时间。我现在就要试试这个解决方案。