DependencyProperty: VS cannot find the source for binding - wpf

I'm trying to bind the Forderground dependency property to my UIControl, so that it's drawn in the color the user wishes. Since myUiControl.Foderground autocopletes, I thought I could just bind it in the XAML file like this:
{Binding ElementName=rootControl, Path=Forderground}
When debugging VS says it cannot find the source for binding with this DependencyProperty.. but I couldn't figure out why this is.
Also how can I list all dependency properties of an object while debugging?

UPDATE: If below wasn't enough for you, try downloading this sample and looking at it.
The ElementName needs to be set as the "x:Name" of your root control and the Path needs to be set to the Property on the root element you wish to bind to. Without the name it cannot find the element you are referring to (hence the initial error) and without the Path it doesn't bind to the correct property (check your output at runtime for an error).
Try this:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid x:Name="root" Background="Green">
<Button Background="White" Margin="100">
<TextBlock Background="{Binding ElementName=root, Path=Background}" Text="TESTING TESTING"/>
</Button>
</Grid>

Can you confirm that your "rootControl" element is defined earlier in the xaml markup than your Binding holder? Usually the Bindings are bound to the earlier declared elements.
If you mean ImmediateWindow and IntelliSense usage while debugging than each dependency property metadata has usually public static access modifiers. You can for instance type "Control." and observe all the corresponding dependency properties, routed events and attached properties members.
Hope this helps.

Related

No Canvas.ZIndex or SetZIndex() property on WPF Canvas

I'm trying to change the Z order of components on my WPF canvas, but it doesn't seem to exist as a XAML property or method in the code behind.
Here's my XAML:
<UserControl x:Class="FrontendUI.Controls.RadialTracker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FrontendUI.Controls"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500">
<Grid >
<Canvas x:Name="TrackerCanvas">
</Canvas>
</Grid>
At first I was trying to do this programmatically:
Then I tried adding a Path using XAML and setting the Canvas.ZIndex, which also didn't exist. There was the Panel.ZIndex though, but I'm assuming this is referring to something higher in the UI (the grid, or User control itself?)
Not sure how to proceed. Does anyone know why this is happening?
Canvas inherits from Panel. Panel.SetZIndex() is a static method and Panel.ZIndex is an attached property that is why you are not seeing it in the xaml.
To set ZIndex for child components in xaml.
<Canvas >
<Button Content="Button" Panel.ZIndex="1" />
</Canvas>
To set Zindex for child components in code behind.
Canvas.SetZIndex(control, index);
You said "There was the Panel.ZIndex though, but I'm assuming this is referring
to something higher in the UI (the grid, or User control itself?)"
Your Assumption is wrong. To achieve your requirement you need to set the Panel.ZIndex
You asked : But they all use Canvas.ZIndex, Does anyone know why this is happening?
Because ZIndex is an attached property (and not a regular dependency property).

Dependency Inheritance for Resources

I've got a somewhat special problem: I have a markup extension that needs to check an attached property. The attached property is defined as inheritable.
This works properly in XAML for all elements that are defined under a Window/UserControl, but not for resources. For example, given this XAML:
<Window MyAttachedProp="...">
<Window.Resources>
<FlowDocument x:Key="Doc">
<Paragraph><Run Text="{MyMarkupExtension}" /></Paragraph>
</FlowDocument>
</Window.Resources>
<FlowDocumentScrollViewer Document="{StaticResource Doc}" />
...
</Window>
The markup extension fails because when the ProvideValue()-function is called, the attached property is not derived from the Window to the FlowDocument and to the Run.
Is there same workaround to make this work?
Thanks,
Steven
From Inheriting Property Values Across Tree Boundaries:
Property inheritance works by traversing a tree of elements. This tree
is often parallel to the logical tree
Apparently a resource dictionary does not constitute an element tree and hence the value won't be inherited. However, i guess it should work when you set the property on the FlowDocument, since that is the root of an element tree:
<Window.Resources>
<FlowDocument x:Key="Doc" MyAttachedProp="...">
<Paragraph><Run Text="{MyMarkupExtension}" /></Paragraph>
</FlowDocument>
</Window.Resources>

Setting design time DataContext on a Window is giving a compiler error?

I have the following XAML below for the main window in my WPF application, I am trying to set the design time d:DataContext below, which I can successfully do for all my various UserControls, but it gives me this error when I try to do it on the window...
Error 1 The property 'DataContext' must be in the default namespace or in the element namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 8 Position 9. C:\dev\bplus\PMT\src\UI\MainWindow.xaml 8 9 UI
<Window x:Class="BenchmarkPlus.PMT.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:UI="clr-namespace:BenchmarkPlus.PMT.UI"
xmlns:Controls="clr-namespace:BenchmarkPlus.PMT.UI.Controls"
d:DataContext="{d:DesignInstance Type=UI:MainViewModel, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="1000" Width="1600" Background="#FF7A7C82">
<Grid>
<!-- Content Here -->
</grid>
</Window>
I needed to add the mc:Ignorable="d" attribute to the Window tag. Essentially I learned something new. The d: namespace prefix that Expression Blend/Visual Studio designer acknowledges is actually ignored/"commented out" by the real compiler/xaml parser!
<Window
...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
...
/>
The following was taken from
Nathan, Adam (2010-06-04). WPF 4 Unleashed (Kindle Locations 1799-1811). Sams. Kindle Edition.
Markup Compatibility
The markup compatibility XML namespace (http://schemas.openxmlformats.org/markup-compatibility/2006, typically used with an mc prefix) contains an Ignorable attribute that instructs XAML processors to ignore all elements/attributes in specified namespaces if they can’t be resolved to their .NET types/members. (The namespace also has a ProcessContent attribute that overrides Ignorable for specific types inside the ignored namespaces.)
Expression Blend takes advantage of this feature to do things like add design-time properties to XAML content that can be ignored at runtime.
mc:Ignorable can be given a space-delimited list of namespaces, and mc:ProcessContent can be given a space-delimited list of elements. When XamlXmlReader encounters ignorable content that can’t be resolved, it doesn’t report any nodes for it. If the ignorable content can be resolved, it will be reported normally. So consumers don’t need to do anything special to handle markup compatibility correctly.
Wow, what a pain! Let's hope MS puts in some VS design-time support for x:Bind.
We to be able to use the VS designer but also be able to switch easily to x:Bind instead of Binding. Here's what I did:
In my View, I added a property to get my ViewModel. This makes sense because x:Bind paths are relative to the Page (i.e. the View object).
In my Page XAML, I added the following to the <Page ... > at the top of the XAML:
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MyView, IsDesignTimeCreatable=False}"
DataContext="{x:Bind}"
This way, the Page's actual data context is set to the Page itself due to the {x:Bind}. That's because x:Bind is relative to the Page and there is no path given.
At the same time, due to the d:DataContext line, the VS designer reflects on the MyView class (without creating an instance) for the purpose of the VS designer interaction. This lets VS design from MyView, where you can then scroll down to the ViewModel property, expand it and select the item that you want to bind to.
When you do all that, the VS designer will create a Binding statement whose path is relative to the View, i.e. it happens to be exactly the same as the path that x:Bind expects. So, if you want to switch to x:Bind later on, you can just search and replace all "{Binding" with "{x:Bind".
Why do we even need the d:DataContext line to tell VS what class to look at? Good question, since you would think that VS could figure out the very next line sets the DataContext to the Page, using DataContext={x:Bind}. Go ahead and try it, it does not work and neither does it work if you change x:Bind to Binding relative to self.
Hopefully this situation will get cleaned up by MS !!
If you are not tooo fussy on the data have a look at the sample data found in xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
You use it like this...
<ItemsControl ItemsSource="{Binding Path=Report.Audit.Data}" d:ItemsSource="{d:SampleData}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
it then renders the items control with a few rows of data
I've solved the problem adding d:DataContext="{d:SampleData}" in the component definition (UserControl or Window).
<UserControl x:Class="TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestApp.Views"
DataContext="{Binding TestViewModel}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:SampleData}"
>

WPF Databinding to Custom Collection Objects

Public Sub New(ByVal log As Entities.LogSystem)
InitializeComponent()
Me.DataContext = log
End Sub
This is the the initializer for my custom control It passes in a single entity that has several property fields. This control is added to a parent control so that it appears in a stackpanel.
Anyway I am trying to get the specific data from this control into several different text boxes:
<UserControl x:Class="LogSystemPickerItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WavelengthIS.WISRED.UserControls"
Width="579" Height="122">
<UserControl.Resources>
<local:LogSystemPickerItem x:Key="Log"/>
</UserControl.Resources>
<Grid DataContext="{Binding Source={StaticResource Log}}">
<Label Height="30" Name="Label1" VerticalAlignment="Top" Content="{Binding deptDescription}"/>
</Grid>
</UserControl>
As you can see i havent really gotten too far. I have tried many different ways to do this including using dependency properties...I just really cant find a tutorial that shows this specific circumstance...can anyone point me in the right direction?
If you're setting the DataContext in the code behind as per your first code snippet, there's no need to also do it in the XAML, so you can remove the "Log" resource and the corresponding DataContext assignment on the Grid.
Once you've done that, it should work assuming there is a deptDescription property on your log class.
... and in XAML you may do it this way...
<UserControl.DataContext>
<local:LogSystemPickerItem/>
</UserControl.DataContext>

Access XAML object from code

I created a UserControl in WPF:
In Xaml:
<UserControl x:Class="OutlookPanel.MailRelation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
xmlns:graph="clr-namespace:MyPanel"
>
<DockPanel>
<graph:Graph Name="theGraph" NodesBindingPath="ChildNodes"
NodeTemplateSelector="{StaticResource nodeTemplateSelector}">
..
</DockPanel>
</UserControl>
I cs:
object theThing = e.Parameter;
((MailRelation)sender).theGraph.CenterObject = theThing;
This last sentence does not work as theGraph is not accessible.
Any idea why i can access theGraph ?
Thanks
John
Name="theGraph"
should be
x:Name="theGraph"
from http://msdn.microsoft.com/en-us/library/ms752059.aspx
x:Name: Specifies a run-time object
name for the instance that exists in
run-time code after an object element
is processed. You use x:Name for cases
of naming elements where the
equivalent WPF framework-level Name
property is not supported. This
happens in certain animation
scenarios.
Probably because that property is private. Provide a public getter and you should be able to get it. In your code, add something like
public Graph TheGraph { get { return theGraph; } }

Resources