【问题标题】:WPF radiobuttons IsHitTestVisible not workingWPF单选按钮IsHitTestVisible不起作用
【发布时间】:2016-10-21 22:23:08
【问题描述】:

我在列表框中使用单选按钮,无法禁用它们。 搜索后,我找到了有关 IsEnabled 和 IsHitTestVisible 的链接。当我将 IsEnabled 设置为 false 时,按钮和文本变为灰色,但它仍然可以点击。设置 IsHitTestVisible 并没有改变这一点。我的 XAMLcode 如下所示:

<ListBox ItemsSource="{Binding Source}" BorderThickness="0" VerticalAlignment="Stretch" Background="Transparent" SelectedItem="{Binding SelectedSource, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <RadioButton IsHitTestVisible="{Binding IsEnabled}"  IsEnabled="{Binding IsEnabled}" Margin="0 2 0 0" FontSize="12" FontFamily="Segoe UI Regular" Content="{Binding name}" GroupName="GroupList"  >
                <RadioButton.Style>
                    <Style TargetType="{x:Type RadioButton}">
                        <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
                    </Style>
                </RadioButton.Style>
            </RadioButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

c# 中计算并返回 bool 的属性。

public bool IsEnabled
{     
    get
    {
        IsEnabledCalculate();
        return isEnabled;
    }
}

关于如何禁用选择的任何想法?

【问题讨论】:

  • INotifyPropertyChange 实施在哪里?如果没有它,此属性将永远不会通知 UI 有更改。

标签: c# .net wpf xaml


【解决方案1】:

ListBox 中的RadioButton 上使用IsHitTestVisible 是一个好的开始。这样,当您单击RadioButton 时,事件会“通过”它并由ListBoxItem 处理(该项目被选中)。然后,您可以通过将IsChecked 属性绑定到ListBoxItemIsSelected 属性来操作它。

但这仍然不足以实现您想要的效果。首先,您将IsEnabled 属性绑定在错误的级别:RadioButton。正如您所注意到的,这仍然允许ListBoxItem 被选中。因此,您需要将IsEnabled 属性绑定到ListBoxItmIsEnabled 属性:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="IsEnabled"  Value="{Binding Path=IsEnabled}"/>
            </Style>
        </ListBox.ItemContainerStyle>

这里还有一个小问题。当您在代码隐藏中更改 IsEnabled 属性时,ListBoxItem 将被禁用,但仍将检查 RadioButton。为了防止这种情况并在项目被禁用时取消选中RadioButton,您可以使用DataTrigger

           <DataTemplate>
                <RadioButton GroupName="GroupList" IsHitTestVisible="False"
                     Margin="0 2 0 0" FontSize="12" FontFamily="Segoe UI Regular" 
                     Content="{Binding name}" >
                    <RadioButton.Style>
                        <Style TargetType="{x:Type RadioButton}">
                            <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsEnabled}" Value="False">
                                    <Setter Property="IsChecked" Value="False"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RadioButton.Style>
                </RadioButton>
            </DataTemplate>

【讨论】:

  • 谢谢,这行得通!现在我只是有一个问题,即无法更新 propertychanged 的​​值,而是一次更新一件事
  • @SEASN 你指的是哪个属性?
猜你喜欢
  • 2018-01-17
  • 2012-08-21
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 2015-11-15
  • 2016-01-06
  • 2014-05-06
  • 1970-01-01
相关资源
最近更新 更多