Question about how to use data binding when "Span" is used in XAML.
-
7 august 2012 00:23
Here is my required result...
I can accomplish this with the below XAML
<TextBlock TextWrapping="Wrap" Width="455"> <Span FontSize="20">6</Span> <Span BaselineAlignment="TextTop" FontSize="14">1/2</Span> </TextBlock>
I can also use just C# code with this
string numerator = "6"; string denominator = "1/2"; textBlock1.Inlines.Add(new Run(numerator) { FontSize = 20 }); textBlock1.Inlines.Add(new Run(denominator) { FontSize = 14, BaselineAlignment = BaselineAlignment.TextTop });Which to me seems easy. But most folks say that I should use data binding. But I'm pretty new to WPF and XAML and can't find any examples of how to do this in this particular way. Since I need two bindings in the same textBlock. By the way, the Numerator and Denominator are the variables I want to use with the bindings.
Is this even possible in this scenario?
Thanks.
- Editat de ASI Tech 7 august 2012 00:24
Toate mesajele
-
7 august 2012 00:45Moderator
To use data binding, you'd write this as:
<TextBlock TextWrapping="Wrap" Width="455"> <Run FontSize="20" Text="{Binding Numerator}" /> <Run BaselineAlignment="TextTop" FontSize="14" Text="{Binding Denominator}"/> </TextBlock>
Note that for this to work, Numerator and Denominator need to be properties on your DataContext object, not fields (hence the capitalization to match standard .NET naming conventions).
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marcat ca răspuns de ASI Tech 7 august 2012 15:19