如何在Xaml中使用工具提示创建RadioButtion的绑定列表? [英] How Can I Create a Bound List of RadioButtions with ToolTips in Xaml?

查看:83
本文介绍了如何在Xaml中使用工具提示创建RadioButtion的绑定列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个逻辑上相关的RadioButton的列表. RadioButton必须绑定用于MVVM.每个单选按钮上都有工具提示.

I want to create a list of logically related RadioButtons. The RadioButtons are bound for MVVM use. There's ToolTips on each RadioButtons.

推荐答案

这里是一种样式,它通过使用ListBox创建一组逻辑上相关的RadioButton. MyClass包含两个String属性:MyName和MyToolTip.该样式将显示RadioButtons列表,包括正确运行的各个工具提示.这是用于MVVM的所有Xaml解决方案.

Here is a Style that creates a group of logically related RadioButtons by using a ListBox. MyClass contains two String properties: MyName and MyToolTip. The style will display the list of RadioButtons including properly functioning individual ToolTips. This is an all bound, all Xaml solution for MVVM use.

示例用法:

ListBox Style ="{StaticResource radioListBox}" ItemsSource ="{Binding MyClasses}" SelectedValue ="{Binding SelectedMyClass}"/>

ListBox Style="{StaticResource radioListBox}" ItemsSource="{Binding MyClasses}" SelectedValue="{Binding SelectedMyClass}"/>

样式:

    <Style x:Key="radioListBox" TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="5" />
    <Setter Property="Background" Value="{x:Null}" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid Background="Transparent">
                                <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}" Content="{Binding MyName}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ToolTip" Value="{Binding MyToolTip}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

这篇关于如何在Xaml中使用工具提示创建RadioButtion的绑定列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆