我正在尝试使用以下 XAML 将IsEnabled控件的IsChecked属性绑定到a 的属性CheckBox,因此将根据CheckBox状态启用或禁用控件。
<Setter Property="IsEnabled" Value="{Binding IsChecked, ElementName=aCheckBox, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
它不起作用。怎么了?
编辑:感谢您的所有评论!下面来自 style.xaml,现在基于@Ivan 的评论。禁用时,TextBlock 设置为“灰色”(取自此处)
<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>
Run Code Online (Sandbox Code Playgroud)
虽然这是一个老问题,但仍然是其他人对标题中原始问题的回答,像我一样在这里绊倒:
<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>
Run Code Online (Sandbox Code Playgroud)