[英]Button inside listview c#
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 最好的问候,安托万
You can bind the CommandParameter
to the ListViewItem
to which the Button
belongs. 您可以将CommandParameter
绑定到Button
所属的ListViewItem
。 Something like: 就像是:
<Button x:Name="button1"
Command="{Binding DataContext.SelectViewCommand,
ElementName=GeneralWindowView}"
CommandParameter="{Binding}">
In this way, the CommandParameter
is always the ViewModel
of the ListViewItem
on which the Button
stays. 这样, CommandParameter
始终是Button
所在的ListViewItem
的ViewModel
。
As a side node, I think that if the SelectViewCommand
deals with the current ListViewItem
, maybe the Command
should stay on the ViewModel
of the ListViewItem
, not the one on the GeneralWindowView
... 作为一个副节点,我认为如果SelectViewCommand
处理当前的ListViewItem
,则该Command
应该保留在ListViewItem
的ViewModel
上,而不是GeneralWindowView
上的GeneralWindowView
...
Any way, hope this helps. 无论如何,希望这会有所帮助。
You could do something like this.... 你可以做这样的事情。
class Friend
{
// Other properties......
public string Link_1 { get; set; }
}
set Link_1 = "ChatViewModel" somewhere 在某处设置Link_1 =“ ChatViewModel”
XAML XAML
<Button x:Name="button1" Command="{Binding DataContext.SelectViewCommand, ElementName=GeneralWindowView}" CommandParameter="{Binding}">
then in your SelectViewCommand Command handler 然后在您的SelectViewCommand命令处理程序中
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... 还有另一种方法可以直接解决在CommandParameter中发送多个参数的问题...
XAML XAML
add the manespace 添加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 这是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 这是您的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). 这是解决问题的正确方法(在CommandParameter中发送2个参数)。 But as you can see, it's rather involved.... 但是正如您所看到的,它确实涉及其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.