Reputation: 779
XAML is a capable of building trees of C# objects, just as you would in code; however, although I can instantiate objects no problem, I am stuck with the syntax for initialising one to the value of another (if this is even possible).
To illustrate, consider the following pointless example:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyClass">
<x:String x:Name="MyString">Hello</x:String>
<x:String x:Name="MyString2">STUCK FOR SYNTAX HERE</x:String>
The strings are instantiated. I can see them in the debugger and I can write code to manipulate them just as if they were normal pieces of C# code. My question is: can I use the value of the first string 'Hello' to initialise the second string? Or, put more generally, can I access the value of one XAML object from another XAML object?
Hope this makes sense.
Upvotes: 0
Views: 384
Reputation: 5163
Your code snippet does not compile. The answer to your question is no, and going out on a limb here, but you're likely misusing XAML. As Dennis said in the comment, you should be using data-binding. The closest thing that matches what you're trying to do is this:
<Window ....
xmlns:sys="clr-namespace:System;assembly=mscorlib" >
<Window.Resources>
<system:String x:Key="MyString">Hello</system:String>
<system:String x:Key="MyString2">STUCK FOR SYNTAX HERE</system:String>
</Window.Resources>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Source="{StaticResource MyString}" />
<Binding Source="{StaticResource MyString2}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Window>
The following code would show "Hello - STUCK FOR SYNTAX HERE" in a text block.
The reason you can't concat strings in XAML is because you're literally initializing a System.String
which does not support any kind of other type markup.
Upvotes: 2