Dot-Net

增加單選按鈕的點擊大小

  • September 5, 2011

我多年來一直在使用 Windows 窗體,但我對 WPF 還是比較陌生。我有許多沒有標籤的單選按鈕(標籤在列的頂部,不用擔心它們!這個程序將在平板電腦上執行,所以我想讓單選按鈕的點擊區域盡可能大盡可能。我還需要單選按鈕位於其列和行的中心。

我可以通過將其添加到網格的每一列來獲得我想要的外觀:

<Label Name="connectedLabel" Grid.Column="2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
   <RadioButton x:FieldModifier="private" Name="connectedRadioButton" Grid.Column="2" Checked="otherRadioButton_CheckedChanged" Unchecked="otherRadioButton_CheckedChanged"></RadioButton>
</Label>

它只是在填充網格部分的標籤中居中一個單選按鈕。

顯然,行為都是錯誤的(事件不會通過,您可以在同一行上選擇多個單選按鈕等)。

這將是 Winforms 中的蛋糕,我希望 WPF 中有一個簡單的解決方案。

有人可以幫忙嗎?

編輯:橙色區域是單選按鈕的預設點擊區域,綠色區域是我想要的點擊區域。到目前為止,如果沒有大量定制接線,這看起來是不可能的

在此處輸入圖像描述

編輯每個有問題的新圖像。

如果你不介意額外的輸入,你可以使用這個:

       <Style TargetType="RadioButton" x:Key="rb">
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="RadioButton">
                       <Grid>
                           <RadioButton IsChecked="{Binding Path=IsChecked, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                           <Border Background="Transparent" />
                       </Grid>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>

這在我的小測試應用程序中按預期工作:

<Grid>
   <Grid.Resources>
       <Style TargetType="RadioButton" x:Key="rb">
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="RadioButton">
                       <Grid>
                           <RadioButton IsChecked="{Binding Path=IsChecked, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                           <Border Background="Transparent" />
                       </Grid>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>

       <Style TargetType="ListBoxItem" x:Key="ics">
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="ListBoxItem">
                       <Grid ShowGridLines="True">
                           <Grid.ColumnDefinitions>
                               <ColumnDefinition />
                               <ColumnDefinition />
                               <ColumnDefinition />
                               <ColumnDefinition />
                           </Grid.ColumnDefinitions>

                           <RadioButton HorizontalAlignment="Center" VerticalAlignment="Center" />
                           <RadioButton HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" />
                           <RadioButton Style="{StaticResource rb}" Grid.Column="2" />
                       </Grid>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>
   </Grid.Resources>

   <ListBox ItemContainerStyle="{StaticResource ics}">
       <ListBoxItem>1</ListBoxItem>
   </ListBox>
</Grid>

看起來像:

在此處輸入圖像描述

(顯然你會想要使用提供的第三種方法)

我知道這看起來並不多,但它會給你你的結果。再次,請原諒額外的打字和缺乏使用的編碼標準。

為此,滑鼠懸停不會產生視覺效果,但命中測試是有效的。我認為只要這將在平板電腦上並且您不跟踪手指就可以了。


如果您只是希望控制項具有更大的尺寸,您可以使用以下方法

RenderTransform您可以通過將屬性設置為ScaleTransform對象來調整控制項的大小。

調整RadioButton容器內所有對象的大小 (視窗、頁面、網格等)

<Window.Resources>
   <Style TargetType="RadioButton">
       <Setter Property="RenderTransform">
           <Setter.Value>
               <ScaleTransform ScaleX="10" ScaleY="10"/>
           </Setter.Value>
       </Setter>
   </Style>
</Window.Resources>

或全部帶鑰匙

   <Style TargetType="RadioButton" x:Key="resizeRadioButton">
       <Setter Property="RenderTransform">
           <Setter.Value>
               <ScaleTransform ScaleX="10" ScaleY="10"/>
           </Setter.Value>
       </Setter>
   </Style>

用法:

<RadioButton Style="{StaticResource resizeRadioButton}" />

或單獨

<RadioButton>
   <RadioButton.RenderTransform>
       <ScaleTransform ScaleX="10" ScaleY="10"/>
   </RadioButton.RenderTransform>
</RadioButton>

但是,如果您想使用更大的控制項和更大的點擊區域的組合(或者對於一個集合類型的所有控制項來說只是更大的點擊區域),您可以使用:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <Style TargetType="RadioButton">
       <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
       <Setter Property="HorizontalAlignment" Value="Center" />
       <Setter Property="VerticalAlignment" Value="Center" />

       <Setter Property="RenderTransform">
           <Setter.Value>
               <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
           </Setter.Value>
       </Setter>

      <Setter Property="Content">
          <Setter.Value>
              <Border>
                  <Rectangle Margin="-10" Fill="Transparent" />
              </Border
          </Setter.Value>
      </Setter>
   </Style>

</ResourceDictionary>

或者只是在另一個容器中使用控制項的預設行為,並使用該HorizontalAlignment="Stretch"屬性,但是我相信這將在左上角繪製控制項。

引用自:https://stackoverflow.com/questions/7252927