Resources as nodes in XAML - wpf

Is there any way to use a XAML resource as a node instead of an attribute? Something like this?
<Window.Resources>
<Button x:Key="TestButton" x:Shared="False" Content="..." />
</Window.Resources>
<my:ButtonBar x:Name="ButtonBar1">
<my:ButtonBar.Buttons>
{StaticResource TestButton}
</my:ButtonBar.Buttons>
<my:ButtonBar>
<my:ButtonBar x:Name="ButtonBar2">
<my:ButtonBar.Buttons>
{StaticResource TestButton}
</my:ButtonBar.Buttons>
<my:ButtonBar>
Obviously this is just an example and obviously this won't work.
But how could I make this work?

This should work:
<my:ButtonBar x:Name="ButtonBar1">
<my:ButtonBar.Buttons>
<StaticResource ResourceKey="TestButton" />
</my:ButtonBar.Buttons>
<my:ButtonBar>

Related

How to Use Usercontrol more than once inside one Control

I created WPF UserControl which enables to enter some information now I have Main entry form where I want to use tow instances of that UserControl When I add my YserControl as Resource and than try to use it as ContrntControl's Content exception is thrown informing that control is already a logical child. Can anyone provide with sollution?
You can use the x:Shared attribute so that whenever something references the resource, a new instance is created instead of it being shared.
Thus you might have something like this:
<Window.Resources>
<MyUserControl x:Key="MyControlKey" x:Shared="False" .... />
....
</Window.Resources>
http://msdn.microsoft.com/en-us/library/aa970778.aspx
http://www.wpfmentor.com/2009/01/how-to-ensure-new-instance-of-resource.html
I just found out way to not throw exception but I want to know if this is right sollution
<TaicoControl:WizardPage Title="Title1"
BackButtonVisibility="Collapsed"
CancelButtonVisibility="Collapsed"
Description="Desctiption1"
PageType="Interior">
<ContentPresenter ContentTemplate="{StaticResource PersonEntryFormTemplate}" DataContext="{Binding Person}" />
</TaicoControl:WizardPage>
<TaicoControl:WizardPage Title="Title2"
BackButtonVisibility="Collapsed"
CancelButtonVisibility="Collapsed"
Description="Description2"
NextButtonVisibility="Collapsed"
PageType="Interior">
<ContentPresenter ContentTemplate="{StaticResource PersonEntryFormTemplate}" DataContext="{Binding Person.ContactPerson}" />
</TaicoControl:WizardPage>

Silverlight: How do I pass True to a CommandParameter?

How do I pass True to a CommandParameter?
Currently I am imperatively adding Boolean.True to the resource dictionary, but that seems like a clumsy way to do it.
Because command parameters are of type 'object' the XAML parser is unable to perform type conversion for you. If you pass 'true', the parser has no way of knowing that you want this converted to a boolean value. You will have to do this explicitly. You could use the property element syntax:
<Button>
<Button.CommandParameter>
<sys:Boolean>true</sys:Boolean>
</Button.CommandParameter>
</Button>
Where the sys namepsace is mapped:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ColinE's answer is fine, but I think it's a bit neater to define the true/false as resources. You only have to do that once:
<UserControl.Resources>
<sys:Boolean x:Key="BoolTrue">True</sys:Boolean>
<sys:Boolean x:Key="BoolFalse">False</sys:Boolean>
</UserControl.Resources>
Then you can reference it as a StaticResource for the CommandParameter:
<Button CommandParameter="{StaticResource BoolTrue}" />
your XAML changes to this.
<Button
Command="{Binding Path=WhateverCommand}"
CommandParameter="{x:Static BooleanHelper.True}" />

wpf - How to use Path, ElementName with markup extension

I am using this markup extension
<DataGridTextColumn Header="Something"
Binding="{controls:SwitchBinding Something, Yes, No}" />
It all works fine, except that now I need to specify Path and Element Name for the Binding.(maybe even 'mode')
I have unsuccessfully tried:
Binding="{controls:SwitchBinding {Binding Path=SelectedItem.SystemDefined, ElementName=dgrdStatementBlocks}, Yes, No}"
Can somebody please point me to the correct way of doing this?
Thanks.
Why would you do this??
Binding="{controls:SwitchBinding {Binding Path=SelectedItem.SystemDefined, ElementName=dgrdStatementBlocks}, Yes, No}
Try the following:
Binding="{controls:SwitchBinding Path=SelectedItem.SystemDefined, ElementName=dgrdStatementBlocks, ValueIfTrue=Yes, ValueIfFalse=No}
Edit:
I tried this in a sample WPF(.Net4) (not Silverlight) application. And the following worked:
<CheckBox Name="CheckBox1"
IsChecked="True" />
<TextBlock Name="TextBlock1"
Text="{local:SwitchBinding ElementName=CheckBox1, Path=IsChecked, ValueIfTrue=Yes, ValueIfFalse=No}" />

Microsoft WPF ribbon - how can I add a RibbonTab stored in a Resources.xaml file?

I have a RibbonTab defined in a Resource file (xaml resources) and i need to add it to the ribbon's tabs collection.
How do i do that? (In xaml)
The Ribbon in xaml is something like that:
<Ribbon>
<Ribbon.Tabs><Ribbon.Tabs/>
</Ribbon>
So it holds a collection of tabs, i don't know how to insert a tab stored in the static resources.
Thank you in advance :)
Teodor
edit: This is the WPF Microsoft ribbon
Edit 2: I tried using <DynamicResource ResourceKey="MyTabKey" /> but i get this error:
Property 'Tabs' does not support
values of type
'DynamicResourceExtension'
First you place the RibbonTabs as resources in whatever ResourceDictionary you want:
<Application.Resources>
<r:RibbonTab Label="Tab_A" x:Key="RibControl_A">
<r:RibbonGroup>
<r:RibbonButton>
<r:RibbonButton.Command>
<r:RibbonCommand LabelTitle="CommandA"/>
</r:RibbonButton.Command>
</r:RibbonButton>
</r:RibbonGroup>
</r:RibbonTab>
<r:RibbonTab Label="Tab_B" x:Key="RibControl_B">
<r:RibbonGroup>
<r:RibbonButton>
<r:RibbonButton.Command>
<r:RibbonCommand LabelTitle="CommandB"/>
</r:RibbonButton.Command>
</r:RibbonButton>
</r:RibbonGroup>
</r:RibbonTab>
</Application.Resources>
Then you can just reference them as StaticResources
<r:Ribbon>
<r:Ribbon.Tabs>
<StaticResource ResourceKey="RibControl_A" />
<StaticResource ResourceKey="RibControl_B" />
</r:Ribbon.Tabs>
</r:Ribbon>
That compiles and runs successfully for me.

How can I define a variable in XAML?

I have the following two buttons in XAML:
<Button Content="Previous"
Margin="10,0,0,10"/>
<Button Content="Next"
Margin="0,0,10,10"/>
How can I define "10" to be a variable so I can change it in one place, something like this:
PSEUDO CODE:
<variable x:key="theMargin"/>
<Button Content="Previous"
Margin="{Variable theMargin},0,0,{Variable theMargin}"/>
<Button Content="Next"
Margin="0,0,{Variable theMargin},{Variable theMargin}"/>
Try this:
add to the head of the xamlfile
xmlns:System="clr-namespace:System;assembly=mscorlib"
Then Add this to the resource section:
<System:Double x:Key="theMargin">2.35</System:Double>
Lastly, use a thickness on the margin:
<Button Content="Next">
<Button.Margin>
<Thickness Top="{StaticResource theMargin}" Left="0" Right="0"
Bottom ="{StaticResource theMargin}" />
</Button.Margin>
</Button>
A lot of system types can be defined this way: int, char, string, DateTime, etc
Note:
You're right... Had to do some better testing.. changed to code so that it should work
Similiar to Sorskoot's answer, you can add a thickness resource to use, thereby defining each margin direction independently
<UserControl.Resources>
<Thickness x:Key="myMargin" Top="5" Left="10" Right="10" Bottom ="5"></Thickness>
</UserControl.Resources>
Then just use the Thickness as the Margin:
<Button Content="Next" Margin="{StaticResource myMargin}"/>
Why don't you try adding the value as a StaticResource?
Resources.Add("theMargin", 10);
Then you can get that value like this:
<Button Content="Previous"
Margin="{StaticResource theMargin},0,0,{StaticResource theMargin}"/>
<Button Content="Next"
Margin="0,0,{StaticResource theMargin},{StaticResource theMargin}"/>
You need invoke this before InitializeComponent or use INotifyPropertyChanged Interface after that
In .NET Core #Sorskoot's answer was working, but produced a warning. So instead I used this namespace declaration:
xmlns:system="clr-namespace:System;assembly=System.Runtime"

Resources