gpt4 book ai didi

c# - 如何将命令从 Page 的 DataContext 绑定(bind)到列表项

转载 作者:行者123 更新时间:2023-11-30 18:32:12 25 4
gpt4 key购买 nike

我有一个页面,一个 View 模型被设置为其数据上下文。在该页面中,我有一个列表。这是通过 View 模型中的属性填充的。列表有一个用户控件。并且该用户控件有一个按钮。我希望该按钮与 View 模型中的命令绑定(bind)。有办法吗?

<Page DataContext=PageViewModel>
...
<ScrollViewer Grid.Row="3" Margin="20,0" Visibility="{Binding ByVenueSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
<StackPanel>
<ItemsControl ItemsSource="{Binding EventsListByVenue}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<myControls:EventDetails /> <!--in this control i want to bind a command available in PageViewModel-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
...
</Page>

最佳答案

在@FunksMaName 的帮助下,我解决了这个问题。我确信有更优雅和更好的方法,但这对我来说是一个快速简单的解决方案:

<Button Style="{StaticResource NoHighlightButtonStyle}" Tag="{Binding link}" CommandParameter="{Binding Path=Tag,RelativeSource={RelativeSource Mode=Self}}" Visibility="{Binding link,Converter={StaticResource DataAvailabilityToVisibilityConverter}}" Command="{Binding Path=Event.LinkCommand,Source={StaticResource Locator}}" >
<TextBlock Margin="0,5" Foreground="SkyBlue" Text="{Binding link}" TextWrapping="Wrap" FontSize="16"/>
</Button>

注意事项:我认为 xaml 仅在命令上下文中搜索命令参数,所以它给了我 null 参数,而相同的绑定(bind)对于 Button 内的文本 block 工作正常。所以我欺骗它将值存储在标签中,并从那里使用它。

Tag="{Binding link}" CommandParameter="{Binding Path=Tag,RelativeSource={RelativeSource Mode=Self}}"

关于c# - 如何将命令从 Page 的 DataContext 绑定(bind)到列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18869092/

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