作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个页面,一个 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/
我是一名优秀的程序员,十分优秀!