I'm facing a binding error mentioned in the output window as follows.
Error:
"System.Windows.Data Error: 40 : BindingExpression path error:
'IsDropDownOpen' property not found on 'object' ''SaveEnterpriseView'
(Name='')'. BindingExpression:Path=IsDropDownOpen;
DataItem='SaveEnterpriseView' (Name=''); target element is
'RibbonButton' (Name=''); target property is 'NoTarget' (type
'Object')"
Here is my xaml code:
<UserControl x:Class="Project1.Views.SaveEnterpriseView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clrnamespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
xmlns:base="clr-namespace:Project1.WPF;assembly=Project1">
<UserControl.Template>
<ControlTemplate>
<ribbon:RibbonButton Command="{BindingSaveEnterpriseCommand}" Label="{base:TextID _SaveEnterprise}" />
</ControlTemplate>
</UserControl.Template>
</UserControl>
From this link WPF Ribbon: DataTemplate causes BindingExpression path error, I assume this problem is related to ribbonbutton itself. But I'm uanble to find the fix for this.
Related
I want to add a variable to my xaml.
My xaml looks like this
<Page
x:Class="UwpTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="using:System"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<system:String x:Key="var1">1.2</system:String>
</Page.Resources>
<Grid>
</Grid>
</Page>
When run, I get runtime error.
Microsoft.UI.Xaml.Markup.ReflectionHelperException
HResult=0x80131500
Message=Error in reflection helper. Please add '<PropertyGroup><EnableTypeInfoReflection>false</EnableTypeInfoReflection></PropertyGroup>' to your project file.. Created Xaml type 'String' has a different name than requested type 'System.String'
Source=Windows
StackTrace:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at UwpTest.MainPage.InitializeComponent() in D:\Dev\UwpTest\UwpTest\obj\x86\Debug\MainPage.g.i.cs:line 33
at UwpTest.MainPage..ctor() in D:\Dev\UwpTest\UwpTest\MainPage.xaml.cs:line 27
I want to add a variable to my xaml
You could add x:String into resource as variable for example
<x:String x:Key="var1">Hello</x:String>
For more please refer to XAML intrinsic data types
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}"
My XAML has been working fine, referencing a Static Property of a static class
<TextBox IsReadOnly="{x:Static loc:StateMachine.IsReadOnly}" />
I now want to refactor the above, so I can use a ResourceDictionary
So, my resource dictionary is
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:stat="clr-namespace:MyProjectHelper"
>
<stat:StateMachine x:Key="StaticResources" />
</ResourceDictionary>
On my XAML, I removed the namespace reference called loc (which I was using) and I add a reference to the resource dictionary (which works since my views show and converters do their job) but when I add the following code
<TextBox IsReadOnly="{StaticResource StaticResources.IsReadOnly}" />
I get a runtime exception
'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '50' and line position '97'.
My research shows this is something wrong wit the XML - it could be a typo (checked) or an unexpected value but I can't see what I need to do to fix this despite looking over SO and Google
I also tried
<TextBox IsReadOnly="{Binding Source={StaticResource StaticResources}, Path=IsReadOnly}" />
but the exception is
'The invocation of the constructor on type 'SeoHelper.StateMachine' that matches the specified binding constraints threw an exception.' Line number '9' and line position '6'.
{StaticResource } binding needs to be able to instantiate your class, and it had problem to do so because the class was static. Your last trial would've worked if only the class wasn't declared as static.
Possible workaround if you need to keep your class static is, you can register the property instead of the class to resource dictionary using StaticExtension :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:stat="clr-namespace:MyProjectHelper"
>
<x:StaticExtension Member="stat:StateMachine.IsReadOnly" x:Key="StaticResources"/>
</ResourceDictionary>
Then binding to the resource can be done as usual :
<TextBox IsReadOnly="{StaticResource StaticResources}" />
I've created an Resourcedictionary where i define my ImageBrush to an jpg picture. I use this ImageBrush on my Path object to fill it. However when I build I get the error Error:
Error HRESULT E_FAIL has been returned from a call to a COM component.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:System;assembly=mscorlib"
xmlns:Model="clr-namespace:MVVMTestApp.Model;assembly=MVVMTestApp.Model"
xmlns:View="clr-namespace:MVVMTestApp.View"
xmlns:ViewModel="clr-namespace:MVVMTestApp.ViewModel">
<c:Double x:Key ="StrokeUserControl">
2
</c:Double>
<ImageBrush x:Key="CountryBackground" ImageSource="../Assets/gra-bakgrund.jpg" Stretch="Fill"/>
And I'm trying to get it to fill in my Path object like this
Fill="{StaticResource CountryBackground}"
I went through the tons of similar posts but nothing seems to work in my case. All I want to do is load a xaml file (located in some other assembly), and iterate through the different elements/objects to check for a certain attribute value.
I could have read it as a simple XML file, but things like styles etc cannot be caught through xml parsing.
After searching a lot,I tried the following two things:
I removed the x:Class=".." from the xaml
I also added the assembly=XBase.UI in this ( as my original xaml didn't have this and I read that when loading dynamically, you need to know specify the assembly)
then I loaded the remaining file as xml stream.
Then I called XamlReader.Load(stream)
This seems to work for people who posted queries, but I get exception
'System.Windows.Markup.XamlParseException : 'The invocation of the constructor on type 'XBase.UI.XControlBase' that matches the specified binding constraints threw an exception.' Line number '6' and line position '55'.
----> System.InvalidOperationException : The calling thread must be STA, because many UI components require this.'
The second thing I tried was to use the XamlReader.Parse and provide the ParserContext along with it.
This is what I did:
var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[] { });
context.XamlTypeMapper.AddMappingProcessingInstruction("UI", "XBase.UI", ""); //assemblyname is empty as my original file doesn't have one
context.XmlnsDictionary.Add("UI", "clr-namespace:XBase.UI;assembly=XBase.UI");
string text = File.ReadAllText(xamlfile);
var object = XamlReader.Parse(xamlfile, context);
This too throws an exception:
'System.Windows.Markup.XamlParseException : 'Cannot create unknown type '{clr-namespace:XBase.UI}XControlBase'.' Line number '1' and line position '2'.
----> System.Xaml.XamlObjectWriterException : 'Cannot create unknown type '{clr-namespace:XBase.UI}XControlBase'.' Line number '1' and line position '2'.'
My original xaml file
<UI:XControlBase x:Class="XBase.UI.XListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:UI="clr-namespace:XBase.UI" Height="Auto" Width="Auto" IsTabStop="False"
mc:Ignorable="d"
Visibility="{Binding IsVisible, Converter={StaticResource VisibilityConverter}}">
<UI:XControlBase.Resources>
<UI:ButtonTemplateSelector x:Key="ButtonSelector" />
</UI:XControlBase.Resources>
<UI:ItemControlWrapper
ItemsSource="{Binding ButtonList}"
ItemTemplateSelector="{StaticResource ButtonSelector}"
IsTabStop="False">
<UI:ItemControlWrapper.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel Orientation="{Binding ListOrientation}" />
</ItemsPanelTemplate>
</UI:ItemControlWrapper.ItemsPanel>
</UI:ItemControlWrapper>
</UI:XControlBase>
Please help me out here. I am not even sure if this is the right way to achieve what I want. If there is another way to list all of the elements of certain kind in a more meaningful way other than
the XmlDocument's GetElementsByTagName, please let me know.
Thanks in advance!