【问题标题】:WPF bind enabled property of one control to a CheckBoxWPF 将一个控件的启用属性绑定到 CheckBox
【发布时间】:2018-08-02 14:43:14
【问题描述】:

我正在尝试使用以下 XAML 将控件的 IsEnabled 属性绑定到 CheckBoxIsChecked 属性,因此将根据 CheckBox 状态启用或禁用控件。

<Setter Property="IsEnabled" Value="{Binding IsChecked, ElementName=aCheckBox, UpdateSourceTrigger=PropertyChanged}" />

它不起作用。怎么了?

编辑:感谢您的所有 cmets!下面来自 style.xaml,现在基于@Ivan 的评论。 TextBlock 在禁用时设置为“灰色”(取自here

<Style x:Key="printCkBox" TargetType="CheckBox">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <ScaleTransform ScaleX="2" ScaleY="2"/>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Center" />
</Style>

<Style x:Key="fileInfoTxtBlkBase" TargetType="TextBlock">
    <Setter Property="TextWrapping" Value="NoWrap"/>
    <Setter Property="FontSize" Value="20"/>
    <Setter Property="Padding" Value="2"/>
    <Setter Property="IsEnabled" Value="{Binding ElementName=printCkBox, Path=IsChecked, NotifyOnSourceUpdated=True}"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
    </Style.Triggers>
</Style>

<Style x:Key="filenameTxtBlk" BasedOn="{StaticResource fileInfoTxtBlkBase}" TargetType="TextBlock">
    <Setter Property="TextAlignment" Value="Left"/>
</Style>

【问题讨论】:

  • 您确定您的绑定可以看到“aCheckBox”吗?你能提供更多代码吗,我们可以看到 aCheckBox 吗??
  • 当你冒充Setter:我可以看看样式的定义吗?
  • 嗯,看起来应该可以了。我创建了示例应用程序,以下代码运行良好:
  • 谢谢!刚刚添加了样式。

标签: c# wpf xaml


【解决方案1】:

虽然这是一个老问题,但仍然是 title 中的原始问题的答案,供其他人使用,像我一样在这里跌跌撞撞:

<DockPanel>
        <CheckBox x:Name="CbxAgree" Content="Agreed" FontSize="21" 
                  Background="Black" Foreground="Black"
                  VerticalAlignment="Center" Margin="8 4 16 4"
                  IsChecked="True" />

        <Button VerticalAlignment="Stretch" 
                Background="White" Foreground="Black" 
                Content="Submit"  FontSize="22"
                IsEnabled="{Binding ElementName=CbxAgree, Path=IsChecked}"/>
</DockPanel>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多