gpt4 book ai didi

c# - ListView C内的按钮

转载 作者:行者123 更新时间:2023-12-03 10:57:49 40 4
gpt4 key购买 nike

我已经成功地在每个列表 View 项中放置了一个按钮。但是我想获得按钮所在的项目。在这种情况下,每个项目都是一个 friend ,并且在单击按钮时我要一个按钮,我现在想知道它是哪个 friend 。我该如何实现?

查看

 <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>

最好的祝福,
安托万

最佳答案

你可以做这样的事情。

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个参数)。但是正如您所看到的,它确实涉及其中。

关于c# - ListView C内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508939/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com