winrt textblock run binding designtime - xaml

my datacontext in designtime
d:DataContext="{d:DesignInstance Type=viewmodel:MainViewModel, IsDesignTimeCreatable=True}"
....
<TextBlock Text="{Binding Path=Driver}">
work fine. Only in:
<TextBlock/>
<Run Text="{Binding Path=Jahr}" />
</TextBlock>
dont work in designtime, why?

NOTE: It IS possible to bind to a <Run /> but it won't render at Design-Time, only Run-time.
This works a treat for binding.
<TextBlock>
<Run Text="{Binding Results.Count}" />
<Run Text=" results for '" />
<Run Text="{Binding SearchTerm}" />
<Run Text="'"/>
</TextBlock>
You can't set the TextBlock Text AND a Run Binding though. It's either one or the other, this works fine, but doesn't render at design time as you say.

Runs do support binding in Windows Runtime but it seems that those Run Controls do not support design time data... :-(

Related

Wrap text nicely in horizontal stackpanel or wrappanel?

I've seen a few other posts about this but haven't seen anything that solves my problem. Basically, I need to have some text (bold and unbold) wrap within a Stackpanel or Wrappanel nicely. Here is a visual:
I need a combination of the two TextBlocks that you see. I've got the first bold TextBlock which contains "Title: " and then on the same line I need the next TextBlock which may or may not wrap. If it does wrap, I need the top line to stay on the top line and then wrap, as if it were all one TextBlock. Here's a made-in-Paint visual of what I need:
Here's my code that I've got so far:
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here " />
</toolkit:WrapPanel>
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :( " />
</toolkit:WrapPanel>
Now, I'm not opposed to this being done in one TextBlock either (if possible). I know that with TextBlock.Inlines I can add a Run of bold text and then another of regular text. The problem with that is Inlines cannot be bound using MVVM (which I'm not using in this demonstration, but will be using in the final code).
So whatever the solution is, I need to be able to set the values of the TextBlocks from the code-behind so that I know it can be done with MVVM.
Does anyone know of a way to achieve this?
Use the RichTextBox control with a different run for every font style.
<RichTextBox>
<Paragraph>
<Run FontWeight="Bold">Title:</Run>
<Run>More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :(</Run>
</Paragraph>
</RichTextBox>
You can bind the Text property of the Run elements to your data context, ex.:
<RichTextBox>
<Paragraph>
<Run FontWeight="Bold"
Text="{Binding Header}"></Run>
<Run Text="{Binding Text}"></Run>
</Paragraph>
</RichTextBox>
Fix Width Property of both StackPanel. this may help you. Problem is if you don't set Width Property it consider as Auto width.
<StackPanel Orientation="Horizontal" Width="400">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here " />
</StackPanel >
<StackPanel Orientation="Horizontal" Width="400">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :( " />
</StackPanel >
This would helpful for you:
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock>
<Run FontWeight="Bold" Text="{Binding Header}"></Run>
<Run Text="{Binding Text}"></Run>
</TextBlock>
</toolkit:WrapPanel>
Have you tried using construction like this?
<TextBlock>
<TextBlock.Inlines>
<Bold>
<Bold.Inlines>
<Run Text="Title: "/>
</Bold.Inlines>
</Bold>
<Run Text="{Binding Text}"/>
</TextBlock.Inlines>
</TextBlock>
It works like a charm if text-block should be bound to just one block of text.

how to style listpickeritem windows phone 8

can someone tell me how to style listpicker elements on fullscreen mode?
this is my code in page xaml:
<toolkit:ListPicker FullModeItemTemplate="{Binding lpkFullItemTemplate}"
ItemTemplate="{Binding lpkItemTemplate}"
x:Name="people"/>
the templates are:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="lpkItemTemplate">
<TextBlock Text="{Binding people_list}" />
</DataTemplate>
<DataTemplate x:Name="lpkFullItemTemplate">
<TextBlock Text="{Binding people_list}"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
i also want to know if there's a way to prevent the listpicker go in full mode. i've tried to set the ItemCountThreshold proprety but i found out that is private in wp8 sdk
thanks in advance
Test it
<toolkit:ListPicker>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate x:Name="ListPickerFullItemTemplate">
<TextBlock FontSize="32" Text="{Binding}"/>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>

Why does TextBlock trims ending spaces from the text?

Here are my TextBlocks:
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock Text="6 or more characters, at least one letter and a number, " FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
<TextBlock Text="no symbols" FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
</StackPanel>
And here is the output (screen shot):
Why does TextBlock trim ending spaces? However, it works fine when there are leading spaces.
It looks like xml:space="preserve" should do the trick (see Preserving Whitespace in XAML) but that doesn't seem to be working in a Windows Store app (it does in WPF).
If you use the non-breaking space character   it does work
<TextBlock Text="6 or more characters, at least one letter and a number,       " ....
I suppose you could try building a converter on the Text property to check for trailing spaces and replace with non-breaking spaces - presuming the truncation that's happening doesn't occur too early.
Solved with <Run /> in a <TextBlock />..
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock FontFamily="Segoe UI" Foreground="#000000" FontSize="13">
<Run Text="6 or more characters, at least one letter and a number, " />
<Run Text="no symbols" />
</TextBlock>
</StackPanel>
And word wrapping still works
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock FontFamily="Segoe UI" Foreground="#000000" FontSize="13"
Width="200" TextWrapping="Wrap">
<Run Text="6 or more characters, at least one letter and a number, " />
<Run Text="no symbols" />
</TextBlock>
</StackPanel>
I would easily use Jim's solution (#160;) if wrapping was not an issue.
In your mind please think about how HTML handles and preserves spaces. This is also how XAML handles and preserves spaces. You would think, of course, that inside a TextBlock it would be more literally handled, huh? Well, it is what it is. At least there's a solution.
Try use xml:space="preserve":
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock xml:space="preserve" Text="6 or more characters, at least one letter and a number, " FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
<TextBlock xml:space="preserve" Text="no symbols" FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
</StackPanel>
I've found a different solution! The \u+A0 works when you ALSO set the IsTextSelectionEnabled.
I don't know why this would be, and it was a total surprise (I added the field because I just discovered it while also working on my 'Why does my text get trimmed in Universal Apps?' problem).
Also U+205F (medium mathematical space) also works in conjunction with IsTextSelectionEnabled.
RichTextBlock seems to preserve both leading and trailing whitespace (in WP 8.1 WinRT):
<RichTextBlock>
<RichTextBlock.Blocks>
<Paragraph >
<Paragraph.Inlines>
<Run Text="trailing " /><Run Text="bbb" /><Run Text=" leading" />
</Paragraph.Inlines>
</Paragraph>
</RichTextBlock.Blocks>
</RichTextBlock>
But it also seems to add an extra space between the runs in addition to the of the ones you specify.

Binding TextBlock foreground to Global.cs variable in xaml

I've been trawling the web trying to find a similar problem but can't find anything that solves my problem.
I have a static class holding global variables, some of these are colour variables which I want to be able to bind in my xaml controls such that I only have to change one variable to change the lot (the reason for global variables).
The class namespace is MyApp.Assets.Resources the class name is Global(.cs).
In my class I have variables named DEFAULTCOLOR, OKCOLOR and ALARMCOLOR.
My xaml:
<UserControl
<!-- ... -->
xmlns:custom="clr-namespace:InspectionDB.Assets.Resources"
>
<UserControl.Resources>
<custom:Global x:Name="global"/> <!-- THIS GIVES AN ERROR SEE BELOW-->
<common:HierarchicalDataTemplate x:Key="Level1" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Type, Converter={StaticResource localTreeImgConverter}}"/>
<TextBlock {Binding...} />
<StackPanel Orientation="Horizontal" Visibility="{Binding Type, Converter={StaticResource localStatusVisibilityConverter}}">
<TextBlock Margin="5,0, 0,0" Text="[ " Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}" />
<TextBlock Text="{Binding Critical}" Foreground="{Binding ALARMCOLOR, Source={StaticResource global}}"/>
<TextBlock Text=" | " Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}"/>
<TextBlock Text="{Binding OK}" Foreground="{Binding OKCOLOR, Source={StaticResource global}}"/>
<TextBlock Text=" ]" Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}"/>
</StackPanel>
</StackPanel>
</common:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
<!-- Where the hierarchical template is used -->
</Grid>
</UserControl>
The part in the Resources throws an error as well as the binding:
Unable to cast object of type 'ApplicationProxy' to type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentNode'.
What is wrong with my code and how can I get it to work?
I found what my problem was, and therefore why I couldn't find any similar situation.
I had public class Global : App - don't ask me why, I'm sure there was a valid reason long ago but it meant it had some kind of funky circular referencing going on.
Problem solved.

Using different databinding sources within ListBox and ContextMenus

Here is the XAML:
<ListBox ItemsSource="{Binding Documents}" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
Ok so the ListBox's ItemSource is bound to the Documents collection in the VM and properly renders the Titles and IDs
The Context Menu's ItemSource is bound to the CategoryList collection in the VM and properly renders the list of categories.
The problem I have is with the Command Binding:
Command="{Binding AddDocumentToCategoryContextMenuCommand}"
Since the ItemSource for the ContextMenu is already set, it tries to get the AddDocumentToCategoryContextMenuCommand from CategoryList. Obviously the command is not there, it is a member of the VM.
I do not want any references to the VMs or Models in the XAML. Everything is constructed using Unity and VM-View is associated in App.xaml:
<Application.Resources>
<DataTemplate DataType="{x:Type vms:FeedViewModel}">
<views:FeedView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vms:DocumentsViewModel}">
<views:DocumentsView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vms:ManagementViewModel}">
<views:ManagementView/>
</DataTemplate>
<DataTemplate DataType="{x:Type dev:DevelopmentViewModel}">
<dev:DevelopmentView />
</DataTemplate>
</Application.Resources>
How can I databind to a member of the VM from within the ContextItem.
Thanks.
UPDATED edit #1 starts Here
Here is the updated xaml (but still not working but some insight gained):
<ListBox ItemsSource="{Binding Documents}" x:Name="Results" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding ElementName=Results, Path=DataContext.AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
I have this example working for a simple example that does not use a ContextMenu. It appears that the ContextMenu (even though attached to the ListBox) is not part of the user control visual tree. The binding always comes back null / not found. I think the ContextMenu, because it is a floating "window" is constructed in its own tree and therefore cannot find the ListBox call "Results" in order to access the ListBox's DataContext.
Any thoughts on this? Any recommendations on how deal with?
Edit #2 Starts Here
In case you are are wondering, figured out the answer to the binding question:
This binding works:
Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}
Hope this helps others with the same question.
One last update for completeness.
In order for the command to know which context menu item was clicked on, I had to change the xaml slightly (silly oversight):
<ListBox.ContextMenu>
<ContextMenu x:Name="Context" ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}"
CommandParameter="{Binding Category.ID}"
Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
Again, hope this helps others.
Use the ElementName property of the binding to specify that. You'd end up with something like this:
Command="{Binding ElementName=ViewModelObject
Path=AddDocumentToCategoryContextMenuCommand}"