Imagebrush ResourceDictionary - xaml

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}"

Related

How do you add varible in UWP xaml?

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

How to position relative to a child element within another control in XAML

In a UWP app, I have a custom UserControl with some child controls (text boxes, labels, etc.). I have a Page that includes that UserControl as a child element. Now, I want to place a button below that UserControl and align it with one of the control's child text boxes using a RelativePanel. I can't figure out a way to access the child element.
For example, I have a custom control:
<UserControl
x:Class="Sandbox.FooControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Sandbox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBox
x:Name="TheTextBoxIWantToAlignWith"
x:FieldModifier="public"/>
</Grid>
And now I want to align with "TheTextBoxIWantToAlignWith":
<UserControl
x:Class="Sandbox.FooParentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Sandbox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<RelativePanel>
<local:FooControl
x:Name="Foo"
RelativePanel.AlignTopWithPanel="True"
RelativePanel.AlignHorizontalCenterWithPanel="True"/>
<Button
RelativePanel.Below="Foo"
RelativePanel.AlignLeftWith="Foo.TheTextBoxIWantToAlignWith"/> <!-- This doesn't work -->
</RelativePanel>
Obviously this doesn't work ("RelativePanel error: The name 'Foo.TheTextBoxIWantToAlignWith' does not exist in the current context."). I tried:
Setting the FieldModifier to public on the child TextBox
Exposing the TextBox as a public property in code-behind
Exposing the TextBox as a DependencyProperty in code-behind
I really want to avoid flattening out the UserControl into the parent page because it can be reused elsewhere. Is there any way to do this?
I ended up working around the issue with code-behind, similar to Lindsay's suggestion. I hooked into the LayoutUpdated event, which calculated and set the margin. Something like this:
private void AlignButton()
{
var transform = Foo.TheTextBoxIWantToAlignWith.TransformToVisual(RootPanel);
var textBoxRightEdge = transform .TransformPoint(new Point()).X + Foo.TheTextBoxIWantToAlignWith.ActualWidth;
var rightMargin = RootPanel.ActualWidth - textBoxRightEdge;
if (Math.Abs(TheButtonIWantToAlign.Margin.Right - rightMargin) >= 1)
TheButtonIWantToAlign.Margin = new Thickness(0, 0, rightMargin, 0);
}

StaticProperties in Resource Dictionary

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}" />

BindingExpression path error: 'IsDropDownOpen' property not found on 'object'

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.

how can i set header templete in pivot item at run time

actually i want to set image on header of every pivot item .....at run time using a resource
<UserControl x:Class="WindowsPhoneApplication7.heder"
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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="18" d:DesignWidth="24" Loaded="UserControl_Loaded">
<UserControl.Resources>
<DataTemplate x:Key="heder_image">
<Image Source="/WindowsPhoneApplication7;component/Images/appbar.feature.settings.rest.png"></Image>
</DataTemplate>
</UserControl.Resources>
it is a resource
now my pivot item are in another cs file
heder dis =new heder();
pivotItem[i] = new PivotItem();
pivotItem[i].Header = (DataTemplate)dis.Resources["heder_image"];
but the header templte r not set ......image r not display
What you're doing wrong here, is that you're setting the Header of a PivotItem to a template, instead of using the Pivot.HeaderTemplate. That way it wouldn't be necessary to generate the items in C# either, but instead databind (via. the Pivot.ItemsSource property) them, and do all the styling in XAML as you're meant to do.
And when that is said, using a image for a Pivot Header, should really be discouraged, as it goes against the platforms default look&feel, and thus against the platforms UI and UX guidelines.
Check if the image's Build Action is set to Content (see properties of the file in VS2010 explorer). If the image is in the same project as the XAML page, use "/Images/appbar.feature.settings.rest.png" instead of "/WindowsPhoneApplication7;component...".
Use this XAML snippet and set your header in XAML:
<controls:Pivot ItemsSource="{Binding MyObjects}" HeaderTemplate="{StaticResource heder_image}">
<controls:Pivot.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>