最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

data binding - WPF Databinding inside <Usercontrol> header not working - Stack Overflow

programmeradmin1浏览0评论

I have a problem where if I databind the visibility of my usercontrol inside the definition of my <Usercontrol> I get an error. However, if I copy/paste the exact same binding definition into the base grid of that user control the binding works fine.

Is there some reason I can't bind directly in the <UserControl> definition?

Doesn't Work:

<UserControl
             <...>
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             Visibility="{Binding ControlVisibility, RelativeSource={RelativeSource AncestorType={x:Type local:TaskItemDisplayControl}}}">    
    <Grid x:Name="BaseGrid">

With Error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='TaskForceSigma.UI.Controls.TaskItemDisplayControl', AncestorLevel='1''. BindingExpression:Path=ControlVisibility; DataItem=null; target element is 'TaskItemDisplayControl' (Name='RootTaskControl'); target property is 'Visibility' (type 'Visibility')

Works(no error):

<UserControl
             <...>
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">    
    <Grid x:Name="BaseGrid"          
          Visibility="{Binding ControlVisibility, RelativeSource={RelativeSource AncestorType={x:Type local:TaskItemDisplayControl}}}">

My best guess is that, when the definition of the <UserControl> is evaluated the relevant binding information is not yet available?

For clarification, this error occurs for both controls added in the designer as well as those instantiated in code.

Also, the code never hits breakpoints inside the ControlVisibility getter UNLESS I add the binding into the BaseGrid element.

I have a problem where if I databind the visibility of my usercontrol inside the definition of my <Usercontrol> I get an error. However, if I copy/paste the exact same binding definition into the base grid of that user control the binding works fine.

Is there some reason I can't bind directly in the <UserControl> definition?

Doesn't Work:

<UserControl
             <...>
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             Visibility="{Binding ControlVisibility, RelativeSource={RelativeSource AncestorType={x:Type local:TaskItemDisplayControl}}}">    
    <Grid x:Name="BaseGrid">

With Error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='TaskForceSigma.UI.Controls.TaskItemDisplayControl', AncestorLevel='1''. BindingExpression:Path=ControlVisibility; DataItem=null; target element is 'TaskItemDisplayControl' (Name='RootTaskControl'); target property is 'Visibility' (type 'Visibility')

Works(no error):

<UserControl
             <...>
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">    
    <Grid x:Name="BaseGrid"          
          Visibility="{Binding ControlVisibility, RelativeSource={RelativeSource AncestorType={x:Type local:TaskItemDisplayControl}}}">

My best guess is that, when the definition of the <UserControl> is evaluated the relevant binding information is not yet available?

For clarification, this error occurs for both controls added in the designer as well as those instantiated in code.

Also, the code never hits breakpoints inside the ControlVisibility getter UNLESS I add the binding into the BaseGrid element.

Share Improve this question asked Jan 29 at 23:49 SINtuitionSINtuition 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

A RelativeSource Binding with AncesterType won't work because the target element TaskItemDisplayControl is not its own ancestor.

Use a Binding with RelativeSource Self instead:

<UserControl ...
    Visibility="{Binding ControlVisibility, RelativeSource={RelativeSource Self}}">

Besides that, it is uncear why your control would have an additional Visibility property at all.

发布评论

评论列表(0)

  1. 暂无评论