listview里面的按钮c#(Button inside listview c#)

编程入门 行业动态 更新时间:2024-10-24 01:53:58
listview里面的按钮c#(Button inside listview c#)

我已成功在每个listview项目中放置一个按钮。 但是我想获得按钮所在的项目。在这种情况下,每个项目都是朋友,并且点击我想要的按钮时有一个按钮。 我怎样才能做到这一点?

视图

<ListView x:Name="dataGrid" ItemsSource="{Binding Friends}" Height="314" BorderThickness="0" SelectedItem="{Binding SelectedItemFriends}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="Resources\Images\ic_status.png" Height="24" Width="18"/> <StackPanel Margin="5" Orientation="Vertical"> <TextBlock FontWeight="Bold" Text="{Binding name}"/> <StackPanel x:Name="RemoveItems" Margin="5" Orientation="Vertical"> <TextBlock Text="{Binding lastLocation}"/> <TextBlock Text="{Binding timestamp}"/> </StackPanel> <StackPanel x:Name="AdditionItems" Margin="5" Orientation="Vertical" Visibility="Collapsed"> <TextBlock Text="{Binding Path=loc.area}"/> <TextBlock Text="{Binding Path=loc.building}"/> <TextBlock Text="{Binding Path=loc.floor}"/> <TextBlock Text="{Binding Path=loc.room}"/> </StackPanel> </StackPanel> <Button Style="{DynamicResource FlatButtonStyle}" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" CommandParameter="ChatViewModel" x:Name="button1" Content="Chat" Margin="10"> <Button.Template> <ControlTemplate> <Image Source="Resources\Images\chat_image.png"/> </ControlTemplate> </Button.Template> </Button> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="true"> <Setter TargetName="AdditionItems" Property="Visibility" Value="Visible"/> <Setter TargetName="RemoveItems" Property="Visibility" Value="Collapsed"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ListView.ItemTemplate> </ListView>

最好的问候,安托万

I've successfully put a button inside each listview item. But I want to obtain the Item on which the button is.. In this case each item is a Friend and has a button on click of the button I want to now which friend it is. How can I achieve this?

VIEW

<ListView x:Name="dataGrid" ItemsSource="{Binding Friends}" Height="314" BorderThickness="0" SelectedItem="{Binding SelectedItemFriends}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="Resources\Images\ic_status.png" Height="24" Width="18"/> <StackPanel Margin="5" Orientation="Vertical"> <TextBlock FontWeight="Bold" Text="{Binding name}"/> <StackPanel x:Name="RemoveItems" Margin="5" Orientation="Vertical"> <TextBlock Text="{Binding lastLocation}"/> <TextBlock Text="{Binding timestamp}"/> </StackPanel> <StackPanel x:Name="AdditionItems" Margin="5" Orientation="Vertical" Visibility="Collapsed"> <TextBlock Text="{Binding Path=loc.area}"/> <TextBlock Text="{Binding Path=loc.building}"/> <TextBlock Text="{Binding Path=loc.floor}"/> <TextBlock Text="{Binding Path=loc.room}"/> </StackPanel> </StackPanel> <Button Style="{DynamicResource FlatButtonStyle}" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" CommandParameter="ChatViewModel" x:Name="button1" Content="Chat" Margin="10"> <Button.Template> <ControlTemplate> <Image Source="Resources\Images\chat_image.png"/> </ControlTemplate> </Button.Template> </Button> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="true"> <Setter TargetName="AdditionItems" Property="Visibility" Value="Visible"/> <Setter TargetName="RemoveItems" Property="Visibility" Value="Collapsed"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ListView.ItemTemplate> </ListView>

Best regards, Antoine

最满意答案

你可以这样做......

class Friend { // Other properties...... public string Link_1 { get; set; } }

在某处设置Link_1 =“ChatViewModel”

XAML

<Button x:Name="button1" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" CommandParameter="{Binding}">

然后在你的SelectViewCommand命令处理程序中

public void OnSelectViewCommand (dynamic obj) { this.Current_ViewModel = this.GetViewModel(obj.Link_1); }

编辑........

还有另一种方法直接解决了在CommandParameter中发送多个参数的问题......

XAML

添加manespace

xmlns:Converters="clr-namespace:CommandParameterExample.Converters"

添加资源

<UserControl.Resources> <Converters:CommandParameter_Converter x:Key="CommandParameter_Converter" /> </UserControl.Resources>

添加隐藏的文本块

<TextBlock Visibility="Collapsed" Name="VM" Text="ChatViewModel"></TextBlock>

这是你的按钮

<Button Content="Chat" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" > <Button.CommandParameter> <MultiBinding Converter="{StaticResource CommandParameter_Converter}"> <Binding/> <Binding Path="Text" ElementName="VM"/> </MultiBinding> </Button.CommandParameter> </Button>

这是CommandParameter_Converter

class CommandParameter_Converter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values.Clone(); // simply return an array } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }

这是你的SelectViewCommand

public void OnSelectViewCommand (object parameter) { var values = (object[])parameter; var FriendObject = (dynamic)values[0]; var ViewModelName = (string)values[1]; }

这是解决问题的正确方法(在CommandParameter中发送2个参数)。 但正如你所看到的,它相当复杂......

You could do something like this....

class Friend { // Other properties...... public string Link_1 { get; set; } }

set Link_1 = "ChatViewModel" somewhere

XAML

<Button x:Name="button1" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" CommandParameter="{Binding}">

then in your SelectViewCommand Command handler

public void OnSelectViewCommand (dynamic obj) { this.Current_ViewModel = this.GetViewModel(obj.Link_1); }

EDIT........

There is another way that directly addresses the issue of sending multiple parameters in the CommandParameter...

XAML

add the manespace

xmlns:Converters="clr-namespace:CommandParameterExample.Converters"

add the Resource

<UserControl.Resources> <Converters:CommandParameter_Converter x:Key="CommandParameter_Converter" /> </UserControl.Resources>

Add a hidden Text Block

<TextBlock Visibility="Collapsed" Name="VM" Text="ChatViewModel"></TextBlock>

Here is your Button

<Button Content="Chat" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" > <Button.CommandParameter> <MultiBinding Converter="{StaticResource CommandParameter_Converter}"> <Binding/> <Binding Path="Text" ElementName="VM"/> </MultiBinding> </Button.CommandParameter> </Button>

Here is the CommandParameter_Converter

class CommandParameter_Converter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values.Clone(); // simply return an array } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }

and here is your SelectViewCommand

public void OnSelectViewCommand (object parameter) { var values = (object[])parameter; var FriendObject = (dynamic)values[0]; var ViewModelName = (string)values[1]; }

This is the correct way to resolve your issue (sending 2 arguments in CommandParameter). But as you can see, it's rather involved....

更多推荐

本文发布于:2023-08-04 11:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   listview   Button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!