【问题标题】:WPF - Keep button enabled even in container is disabledWPF - 即使在容器中禁用按钮也保持启用状态
【发布时间】:2009-12-14 07:46:37
【问题描述】:

我在 ListBoxItem 中有一个按钮。即使列表框项被禁用,我也希望按钮保持启用状态。这可能吗?

这是我的列表框、列表框项和按钮(称为 btnPick)的样式代码。

<Window.Resources>
 <Style x:Key="CheckBoxListStyle" TargetType="ListBox">
  <Style.Resources>
   <Style TargetType="ListBoxItem">
    <Setter Property="Template">
     <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
       <Grid ScrollViewer.CanContentScroll="True" Margin="2">
        <Grid.ColumnDefinitions>
         <ColumnDefinition Width="20" />
         <ColumnDefinition Width="50" />
         <ColumnDefinition Width="*" />
         <ColumnDefinition Width="30" />
        </Grid.ColumnDefinitions>
        <CheckBox VerticalAlignment="Center" Grid.Column="0" IsChecked="{Binding IsSelected,
           RelativeSource={RelativeSource TemplatedParent},
           Mode=TwoWay}" Name="chkSelectedCheckBox" />
        <TextBlock VerticalAlignment="Center" Grid.Column="1" Margin="5,0,5,0" Text="{Binding Id}" />
        <TextBlock VerticalAlignment="Center" Grid.Column="2" Margin="5,0,5,0" Text="{Binding Title}" />

        <!--This is the one that I want to stay enabled somehow-->
        <Button HorizontalAlignment="Right" x:Name="btnPick" Grid.Column="3" Tag="{Binding Id}" Margin="5,0,5,0"/>                     
       </Grid>                                        
      </ControlTemplate>                              
     </Setter.Value>                                  
    </Setter>                                        
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource DisableWorkItemConverter}">
                    <Binding ElementName="MainForm" Path="PickedWorkItemID"/>
                    <Binding Path="Id"/>
                    <Binding ElementName="btnCreateLink" Path="IsClicked"></Binding>
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="IsEnabled" Value="False"/>
            <Setter Property="loc:Main.IsCurrentItemEnabledChanged" Value="True"/>
        </DataTrigger>
    </Style.Triggers>
   </Style>                                           
  </Style.Resources>                                  
 </Style>                                             
</Window.Resources>                                   

现在我的数据触发器为整个订单项设置了 IsEnabled。是否有可能以某种方式访问​​ ListBoxItem 的子项?

类似:

<Setter Property="chkSelectedCheckBox.IsEnabled" Value="False"/>

是我正在寻找的(但这不起作用)。 (我认为 TargetName 可能会帮助我,但这仅适用于 DataTemplates。)

感谢您的帮助。

【问题讨论】:

    标签: wpf xaml wpf-controls


    【解决方案1】:

    不要对 ListBoxItem 说 IsEnabled="false",而是将内部元素的 IsEnabled 属性设置为 false,如 CheckBox 和所有。无论如何,这将使您的 Button 保持启用状态。

    【讨论】:

    • 感谢您的建议,但我该怎么做呢?我更新了我的代码以显示我的 DataTrigger。如果您知道一种修改方法以允许我仅禁用 ListBoxItem 子项的特定部分,我很想听听。
    • 我想出了如何做到这一点(我将 DataTrigger 移动到 ControlTemplate.Trigger 部分)。它禁用了该复选框,但单击它仍然使其选中
    • 这是由于在 CheckBox 的 IsChecked 属性上设置了带有 IsSelected 属性的绑定。您可以手动删除 Binding 并更新 IsChecked 属性,也可以在 Binding 中使用 UpdateSourceTrigger="Explicit" 并调用 UpdateTarget 方法
    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2014-05-13
    • 2014-04-23
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多