WPF单选按钮击中区域
本文关键字:区域 单选按钮 WPF | 更新日期: 2023-09-27 18:08:02
嗨,我还有一个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
内容。这不是一个好主意,因为它使我的应用程序运行缓慢。
试试这个:
<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
相冲突。不幸的是,我真的无能为力。