【问题标题】:WPF Radio button hit areaWPF 单选按钮点击区域
【发布时间】:2014-06-11 11:03:10
【问题描述】:

您好,我遇到了 WPF 控件的另一个问题。我有一个代码:

<ListBox Margin="0, 5, 0, 0" ItemsSource="{Binding mySource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- Rabio template -->
            <RadioButton GroupName="radiosGroup"
                 Margin="10, 2, 5, 2"
                 Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedSetting}"
                 CommandParameter="{Binding SomeId, Mode=OneWay}"
                 Content="{Binding FileNameWithoutExtensions, Mode=OneWay}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

问题是当我点击RadioButton 或其标签时,它会被选中。没事儿。但是ListBoxItem 宽度大于整个RadioButton 命中区域,当我单击控件右侧时-ListBoxItem 选择但其子RadioButton 没有。以及如何扩大RadioButton的命中区域?

我尝试的一个想法是将Label 添加为RadioButton 内容。这不是最好的主意,因为它让我的应用运行缓慢。

【问题讨论】:

    标签: c# wpf xaml wpf-controls


    【解决方案1】:

    试试这个:

    <ListBox Margin="0,5,0,0" ItemsSource="{Binding mySource, Mode=OneWay, 
        UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single"
        HorizontalContentAlignment="Stretch"> <!-- New Property Added Here -->
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- Rabio template -->
                <RadioButton GroupName="radiosGroup"
                     Margin="10, 2, 5, 2"
                     Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedSetting}"
                     CommandParameter="{Binding SomeId, Mode=OneWay}"
                     Content="{Binding FileNameWithoutExtensions, Mode=OneWay}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    你说:

    不幸的是,这不起作用

    很遗憾,你误会了。我已经简化了示例,并且刚刚测试了它是否可以完美运行(至少在 Visual Studio 2010 和 .NET 4 中):

    <ListBox ItemsSource="{Binding Collection}" HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <RadioButton GroupName="Group" Content="{Binding SomeProperty}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    只需在新项目中试用此代码,您就会发现它确实按预期工作。因此,如果它在您当前的项目中不起作用,那么您有一些其他代码与此ListBox 有某种冲突。不幸的是,我真的无能为力。

    【讨论】:

    • 好的,没错。它有效,但只是部分有效。仍在寻找其他可行的解决方案。
    • 它工作,但只是部分...在我的测试应用程序中,它完全按照您的要求工作。我可以单击ListBoxItem 中的任意位置以选中或取消选中RadioButton。为什么这是不可接受的,或者你能解释一下什么只是部分起作用?
    • 我想说的是,部分解决了这个问题 - 它有效,但不适用于我的情况。
    • 好吧,如果我已经向您展示了代码,当它不在有干扰的项目中时确实可以工作,那么我能做些什么来提供帮助?您的代码中的其他地方有问题。这是你的代码,所以你是唯一知道那是什么的人。就我而言,你问了一个我回答正确的问题。这个问题有一个正确的答案,所以你应该accept it 让其他用户知道这是你在ListBoxItem 中拉伸RadioButton 的方式。
    • 那么我建议您按照帮助中心的How to create a Minimal, Complete, and Verifiable example 页面中给出的建议创建一个实际上 演示您的问题的小代码示例 - 此代码中的代码question 没有这样做,因此您的问题永远无法从该代码中解决。一旦你在一个新的小项目中重现了你的问题,然后用你的新代码示例提出一个新问题。那么我们应该能够帮助您解决这个问题。不过现在,这个问题已经得到了正确的回答,所以应该被接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 2011-07-08
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多