ElementName binding doesn't resolve properties - xaml

When I bind something to the SelectedItem of a ListView using ElementName, the properties cannot be resolved.
I can still build and run my app, but because of this error I'm not sure that I didn't make any typo's.
I read and tried the answer given in ElementName Binding is failing but Source={x:Reference ...} doesn't seem to exist in the WinRT framework.
Just an example:
My page has a property MyContainerObject with an IEnumerable<MyParentObject> inside called MyParents. Each MyParentObject has an IEnumerable<MyChildObject> called MyChildren inside.
The DataContext of my page:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
I have ListView with the following XAML properties:
x:Name = "FirstListView"
ItemsSource = {Binding MyContainerObject.MyParents}
Then I have another ListView with the following XAML properties:
x:Name = "SecondListView"
ItemsSource = "{Binding SelectedItem.MyChildren, ElementName=FirstListView}"
Now ReSharper gives me the following warning:
Cannot resolve property 'MyChildren' in data context of type 'object'.

Add this to your listview:
d:DataContext="{d:DesignInstance model:MyParentObject}"

Related

Binding from Attached Property to Attached Property

I have a situation where I need to add binding like so:
<DataTemplate x:key="bla">
<views:MyView props:special.MyAttachProperty="{Binding props:special.MyAttachProperty}" />
</DataTemplate>
So the same attache property value will be both,
and change according to changes on the data in the viewmodel.
But this binding fails at run time:
[0:] Binding: 'props:special' property not found on '(my data)', target property: 'views:MyView.MyAttachProperty'
How do I make this work?

Binding to xaml page's DataContext in Windows Universal App

I'm using a content dialog do display instance data when an item in a grid is selected.
In the calling page's view model, when an item is selected the following method is executed.
public virtual void ItemSelected(object sender, object parameter)
{
var arg = parameter as Windows.UI.Xaml.Controls.ItemClickEventArgs;
var clickedItem = arg.ClickedItem;
var item = clickedItem as ItemsModel;
var dialog = new ItemsDialog();
dialog.DataContext = item;
dialog.ShowAsync();
}
This shows the dialog, and the content is displayed as expected. Now I'm trying to split my xaml into different templates and I'm trying to use a ContentControl to display the appropriate template. I've written a DataTemplateSelector to help choose the correct template, but now I cannot figure out the data binding for the ContentControl (see simplified version below).
<ContentDialog.Resources>
<UI:MyTemplateSelector x:Key="MyTemplateSelector"
Template1="{StaticResource Template1}"
Template2="{StaticResource Template2}"/>
<DataTemplate x:Key="Template1"/>
<DataTemplate x:Key="Template2"/>
</ContentDialog.Resources>
<StackPanel>
<ContentControl DataContext="{Binding}"
ContentTemplateSelector="{StaticResource MyTemplateSelector}"/>
</StackPanel>
When debugging into my ContentTemplateSelector, my binding is always null. I've tried various forms of the binding syntax with no luck. How do I properly set the DataContext of the ContentControl to that of the ContentDialog?
When debugging into my ContentTemplateSelector, my binding is always
null
You need to set data binding for the Content property of ContentControl control, see Remarks in MSDN:
The Content property of a ContentControl can be any type of object,
such as a string, a UIElement, or a DateTime. By default, when the
Content property is set to a UIElement, the UIElement is displayed in
the ContentControl. When Content is set to another type of object, a
string representation of the object is displayed in the
ContentControl.
So the following xaml should work:
<StackPanel>
<ContentControl Content="{Binding}"
ContentTemplateSelector="{StaticResource MyTemplateSelector}"/>
</StackPanel>
Check my completed sample in Github
You have to bind Content also.
Content="{Binding}"
You have the data source (DataContext) and how the data is displayed (templates) and now you need to specify which of the properties brings that together.

Silverlight - XamlParseException The type 'Type' was not found

The type 'Type' was not found. [Line: 7 Position: 21]
I'm trying to dynamically generate a datatemplate. it works fine, but if I include this attribute, I get the above exception.
Width="{Binding Path=ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}"
And the complete method:
public DataTemplate GetTextColumnTemplate(int index)
{
string templateValue = #"
<DataTemplate
xmlns:sys=""clr-namespace:System;assembly=mscorlib""
xmlns:telerik=""http://schemas.telerik.com/2008/xaml/presentation""
xmlns=""http://schemas.microsoft.com/client/2007""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<StackPanel>
<TextBox Width=""{Binding Path=ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}"" Text=""{Binding Path=V" + (index + 1).ToString() + #",Mode=TwoWay}"" AcceptsTab=""True"" AcceptsReturn=""True""/>
</StackPanel>
</DataTemplate>";
return (DataTemplate)XamlReader.Load(templateValue);
}
You have a Silverlight project. Silverlight does not support the markup extension x:Type. Ancestor bindings in Silverlight look like this:
{Binding Path=Foo, RelativeSource={RelativeSource AncestorType=UserControl}}
[Edit]
And btw you can't bind to ActualWidth. You have to observe the SizeChanged event and have some handling code. You will find quite elegant solutions to this problem here: binding-to-actualwidth.
The error is caused because the XAML parser can't resolve the type x:Type in XAML to a valid CLR type, probably because namespace mappings in XAML cannot be properly processed by the XAML reader without proper context.
I have a customized version of this which uses a ParserContext to define the XML namespace mappings for XAML:
var context = new ParserContext {XamlTypeMapper = new XamlTypeMapper(new string[0])};
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
//... And so on add other xmlns mappings here.
var template = (DataTemplate) XamlReader.Parse(yourXAMLstring, context);

Invalid XAML Exception in ItemTemplate with Telerik DataBoundListBox

I am attempting to use a Telerik RadDataBoundListBox with a custom ItemTemplate. However, when I attempt to set the DataTemplate I receive an Invalid XAML exception and can no longer compile. Should I be using Styles?
<telerikPrimitives:RadDataBoundListBox Grid.Row="1">
<telerikPrimitives:RadDataBoundListBox.ItemTemplate>
<DataTemplate>
<telerikPrimitives:RadHubTile/>
</DataTemplate>
</telerikPrimitives:RadDataBoundListBox.ItemTemplate>
</telerikPrimitives:RadDataBoundListBox>
This error occurs no matter what is inside the DataTemplate tags.

Metro Xaml binding to DataContext.Object.Property not working

I am setting the datacontext of my xaml page to a viewmodel passed in to the construrctor. My viewmodel has an object called Item, which has a property called Category.
public DataEntry(DEViewModel vm)
{
InitializeComponent();
this.vm = vm;
this.DataContext = this.vm;
}
I am trying to bind to the ViewModel.Item.Category property like so:
<TextBox Name="txtCategory" Text="{Binding Path=Item.Category, Mode=TwoWay}" />
This does not work. If I set the datacontext to vm.Item, and bind to Category it works.
Any ideas on how to bind to a property that is hanging off of an object on the viewmodel?
Thanks, Terrence
Do it like this,
<TextBox Name="txtCategory"
Text="{Binding Category, Mode=TwoWay}"
DataContext="{Binding Item}" />
The reason it was not working is because it will only look at property notifications for the DataContext, so just set the local DataContext for the control to Item and the control will handle property notifications for Item.
Thanks,
Alex.