Hyperlinks are staying inactive after setting isEnabled=true to parent control

≡放荡痞女 提交于 2019-12-30 04:03:33

问题


I've got a TabItem contanining a listbox, which has an obeservable collection of my feeds class as its item source. When I refresh/load the feeds into the collection I want to disable the main window so that the user can't go clicking other things while this process is running. So I set tbCtrl.isEnabled=false; to my tab control on the form. Then assign an event handler to the a custom finish event which is triggered after all the feeds are loaded.

This all works fine, however the hyperlinks for the results which are currently displayed on the tab control never get re-enabled (Nor do the next few which are out of view due to the list box size). All the other results further down are fine, as are the results on the other tab.

I've tried calling InvalidateVisual on the tab control after everything is finished, to see if that makes a difference but that doesn't seem to cause any change.

I could understand it if it was all Hyperlinks doing it, or just the ones currently displayed, but I don't understand why ones which are out of scroll are not working either.


回答1:


I found the answer for my case of the hyperlink not getting re-enabled, not sure if it applies to yours:

I found that when the Hyperlink's parent control is disabled (IsEnabled=false), the Hyperlink will not get notified of changes, e.g. IsEnabledChanged does not get fired, even when the bound property changes value.

My solution was to change my Xaml to no longer disable the ancestor control (which was causing the Hyperlink's parent to be disabled). With the parent (TextBlock) always enabled, now Hyperlink updates properly always.

(I'm a little bothered that the IsEnabled binding behaves differently than Controls do, and I'm not sure what I would do if I couldn't leave the ancestor enabled... but at least this lets me understand the issue I was having, and lets me work around it.)

Details: WPF 3.5 SP1




回答2:


I hit the same issue.

What I did is to bind HyperLink's IsEnabled to the parent, and put that in an App global resource.

<Style TargetType="{x:Type Hyperlink}">
    <Setter Property="IsEnabled" Value="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type FrameworkElement}}}" />
</Style>



回答3:


It's not just HyperLinks. It seems to be more specifically TextBlock which of course is what you use to wrap a HyperLink in WPF. This will give the same issue :

<TextBlock>
    <Run Text="Barcode:"/>
    <InlineUIContainer BaselineAlignment="Center">
        <TextBox Text="{Binding OriginalPackage.BarcodeNumber}" />
    </InlineUIContainer>
</TextBlock>

I was hoping setting IsEnabled="True" would fix it but it doesn't seem to.

The easy solution is to use a StackPanel with Orientation="Horizontal"



来源:https://stackoverflow.com/questions/2499415/hyperlinks-are-staying-inactive-after-setting-isenabled-true-to-parent-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!