开发者

WPF Binding to Window property failing

I've got a custom control class in my project called "CarSystemWindow". It descends from Window and has a custom template that gives all windows in my application the same look. It also defines two dependency properties named DeviceName and DeviceType. These are of type string. They default to "Vehicle: " and "Car 54", respectively.

In my main program, I retrieve a row from my database into a View Model object and save that in a normal CLR property called Site during the program's initialization. In the MainWindow's xaml, I have the following code:

<cs:CarSystemWindow x:Class="....MainWindow"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:c="clr-namespace:..."
                    xmlns:cs="..."
                    Background="Black" 
                    Closed="Window_Closed" 
                    DataContext="{Binding Path=Site, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                    DeviceName="{Binding Path=SiteName}" 
                    DeviceType="{Binding Path=SiteTypeName}"
                    Icon="..." 
                    Height="600" 
                    Loaded="Window_Loaded" 
                    ResizeMode="CanMinimize" 
                    SourceInitialized="Window_开发者_开发知识库SourceInitialized" 
                    Title="Window Title" 
                    Width="800" 
                    WindowStartupLocation="CenterScreen">

At run time, the binding on the DataContext attribute is failing with the following message:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=Site; DataItem=null; target element is 'MainWindow' (Name=''); target property is 'DataContext' (type 'Object')

I'm using this same binding code elsewhere and it works. I've even turned the Site property into a dependency property and its still failing.

Does anyone give have ideas why the binding is failing?

Thanks

Tony


I think you need to change your binding to this:

{Binding RelativeSource={RelativeSource Self}, Path=Site}

The reason your current binding is not working is that you are trying to go up a level in the hierarchy from the Window, but you actually want the Window.

Here is a good source for figuring out what the binding string should be for different scenarios:

http://www.nbdtech.com/Free/WpfBinding.pdf


The problem with the DataContext binding is that the line is saying to use the Site property on an object that is an ancestor of this object, and of type Window. Since this object is a Window already, and therefore at the root of the visual tree, there are no ancestors to search and find the specified property.

Why don't you assign the DataContext where this object is constructed?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜