【发布时间】:2021-10-09 05:53:34
【问题描述】:
我的 UWP 应用程序中有一个模板化控件,其中包含一个 ListView。 ListView 在运行时填充。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Renderer"
xmlns:triggers="using:Microsoft.Toolkit.Uwp.UI.Triggers">
<Style x:Key="RendererDefaultStyle" TargetType="local:Renderer" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:Renderer">
<Grid>
....
<ListView x:Name="AnnotsList" Margin="0,12,0,0" SelectionMode="None" Grid.Row="1" VerticalAlignment="Stretch" IsItemClickEnabled="True" Visibility="{Binding IsAnnotationsListOpen, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ItemContainerStyle="{StaticResource AnnotationsListViewItemStyle}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding}" />
<TextBlock Text="{Binding DisplayTitle}" Margin="20,0,0,10" FontSize="12" TextWrapping="WrapWholeWords" Visibility="Visible" />
</StackPanel>
<CommandBar Grid.Column="1">
<CommandBar.SecondaryCommands>
<AppBarElementContainer>
<StackPanel Orientation="Horizontal">
<Button x:Name="btn_RemoveFromList" DataContext="{Binding}">
<Button.Content>
<SymbolIcon Symbol="Delete" />
</Button.Content>
<ToolTipService.ToolTip>
<ToolTip Content="Delete" Placement="Mouse" />
</ToolTipService.ToolTip>
</Button>
</StackPanel>
</AppBarElementContainer>
</CommandBar.SecondaryCommands>
</CommandBar>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle >
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border AutomationProperties.Name="{Binding Key}">
<TextBlock Text="{Binding Key}" Style="{ThemeResource TitleTextBlockStyle}"/>
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
....
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:Renderer" BasedOn="{StaticResource RendererDefaultStyle}"/>
</ResourceDictionary>
我尝试将点击事件绑定到这样的按钮,但由于它是动态生成的,所以它不起作用。
public sealed class Renderer: Control, IDisposable
{
....
private void UpdateAnnotationsListView()
{
(GetTemplateChild("AnnotsList") as ListView).ItemsSource = null;
var source = AnnotationAdapter.GetGroupedAnnotations(); // ObservableCollection<ListViewGroupInfo>
var viewSource = new CollectionViewSource
{
IsSourceGrouped = true, Source = source
};
(GetTemplateChild("AnnotsList") as ListView).ItemsSource = viewSource.View;
if (viewSource.View.Count > 0)
{
(GetTemplateChild("btn_RemoveFromList") as Button).Click -= null;
(GetTemplateChild("btn_RemoveFromList") as Button).Click += async delegate(object sender, RoutedEventArgs e)
{
await OpenRemoveConfirmationAsync();
};
}
}
....
}
列表源是ObservableCollection 类型
public class ListViewGroupInfo: List < object >
{
public ListViewGroupInfo() {}
public ListViewGroupInfo(IEnumerable < object > items): base(items) {}
public object Key
{
get;
set;
}
}
列表源的结构使得我可以对列表项进行相应的分组。
删除按钮是我在这里尝试使用的按钮。
我想将一个方法绑定到 ListView 中这些按钮的点击事件。
我不能使用名称属性,因为随着列表的增长可能会有多个按钮。
由于此按钮位于模板化控件中并在运行时生成,因此我找不到将方法绑定到单击事件的方法。
我的猜测是我必须将命令绑定到按钮。但我也找不到办法。
我没有在模板化控件中使用 MVVM 模式。
谁能帮我解决这个问题?非常感谢任何帮助。
【问题讨论】:
-
这里我们需要更多上下文,你想绑定到点击事件的命令或方法在哪里?最简单的方法是将命令定义为列表中元素的视图模型上的属性,但您的 xaml 片段甚至不描述列表源。请包含足够的视图模型或代码,用于定义单击操作的位置以及它们之间的链接。
-
@ChrisSchaller 我已经更新了这个问题。希望现在有足够的信息来了解我遇到的问题。
-
我明白了,可以做到,但是有很多方法,具体取决于您通常使用的 MVVM 工具包。 (因为我不想使用 MVVMLight 概念,如果您使用的是 basic 或 prism 或已推出自己的)。最终,最好不要将该级别的详细信息放在模板控件中(列表项内容的定义),而是将各个列表项元素模板化。我会看看我是否可以提出一个最小的解决方案。
-
你把List source放在哪里了?
-
如果你使用用户控件来接近,更好的方法是创建依赖属性。更多信息请参考document。
标签: xaml uwp uwp-xaml template-control