简体   繁体   English

Silverlight Xaml覆盖控件的IsEnabled属性

[英]Silverlight Xaml Overriding IsEnabled Property of Control

I have a Silverlight project. 我有一个Silverlight项目。 In the project I have a UserControl. 在项目中,我有一个UserControl。 IsEnabled for this UserControl is set dynamically and most of the time it is set to false. 此UserControl的IsEnabled是动态设置的,大多数情况下将其设置为false。

In this UserControl, I have a several nested grids and all the grids inherit the IsEnable from their parent (UserControl) as usual. 在此UserControl中,我有几个嵌套的网格,并且所有网格都照常从其父级(UserControl)继承IsEnable。

However, for one of the grids inside this UserControl, I don't want to inherit this feature. 但是,对于此UserControl内部的网格之一,我不想继承此功能。 I want to set it True always. 我想一直将其设置为True。 Is there a way to override the IsEnabled feature? 有没有方法可以覆盖IsEnabled功能?

What I tried is, I wrap with ContentControl and I set IsEnabled to True, but it did not work. 我尝试的是,用ContentControl包装,并将IsEnabled设置为True,但是它不起作用。

<ContentControl IsEnabled="True">
  <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="0,0,0,0" BorderThickness="0">
    <HistoryControls:EditableMAMGridContainer StateInfo="{Binding Path=StateInfo, Source={StaticResource Evm}}" />
  </ScrollViewer>
</ContentControl>

The root is UserControl for this Scrollviewer and it has several parents and grandparents. 根是此Scrollviewer的UserControl,它具有多个父级和祖父母级。 It is in the very deep. 它在很深的地方。

EDIT: This code works but vice-versa does not work 编辑:此代码有效,但反之亦然

<UserControl IsEnabled="True">
   .
     .
        <ContentControl IsEnabled="False">
          ...

when UserControl sets False, and contentControl sets True (opposite case of the above code) then does not work. 当UserControl设置为False,而contentControl设置为True(与上述代码相反)时,则不起作用。

First of all: if you disable the parent, all the children are disabled. 首先:如果禁用父级,则所有子级均被禁用。 Period. 期。 You cannot change this, and this is actually a good thing. 您无法更改,这实际上是一件好事。 Because this way you can be absolutely sure to reliably block all user input to an entire subtree of the UI. 因为这样,您可以绝对确定可靠地阻止所有用户输入到UI的整个子树。

Don't try to bend the concepts of the framework. 不要试图改变框架的概念。 The concept here is: disabled absolutely means there cannot be any user input! 这里的概念是:禁用绝对意味着没有任何用户输入! In no part of it, not the tiniest corner of that subtree. 在任何地方,都不是该子树的最小角落。

If we follow this concept then it will quickly become clear to us that you actually don't want your parent to be disabled, because obviously you want a small portion of it to still receive user input. 如果我们遵循这个概念,那么我们很快就会发现您实际上不希望您的父母被禁用,因为显然您希望它的一小部分仍能接收用户输入。 So -conceptually- it is not disabled! 因此-从概念上讲-它不是禁用的!

Now how to translate that technically? 现在如何进行技术翻译?

It means you will have to structure you tree in a way that lets you disable one branch of it while at the same time have a sibling branch still be enabled. 这意味着您将必须以某种方式构造树,以使其禁用一个分支,同时仍启用同级分支。

Example

<TravelBooking>

    <!-- this will be disabled if we don't travel by plane -->
    <TransferToAirportPlanner/>
    <FlightToDestinationPlanner/>
    <ReturnFlightPlanner/>
    <TransferFromAirportPlanner/>

    <!-- this will always be enabled -->
    <HotelPlanner/>

</TravelBooking>

Restructured tree to allow for disabled subtree 重组树以允许禁用子树

<TravelBooking>

    <!-- this will be disabled if we don't travel by plane -->
    <AirplaneTravelPlanningControl>
        <TransferToAirportPlanner/>
        <FlightToDestinationPlanner/>
        <ReturnFlightPlanner/>
        <TransferFromAirportPlanner/>
    </AirplaneTravelPlanningControl>

    <!-- this will always be enabled -->
    <HotelPlanner/>

</TravelBooking>

Now you can easily disable the AirplaneTravelPlanningControl to disable the contained subtree. 现在,您可以轻松禁用AirplaneTravelPlanningControl来禁用包含的子树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM