WPF Databound Label Design Time Text - wpf

I am playing about with WPF and databinding a Label control's content:
<Label Content="{Binding Name}" />
This works a treat, however I'd like to get some text in there at design time so I can see the label. Anyone know how to do this, seems it should be simple.
Thanks
TJ

Try this:
<Label Content="{Binding Name, FallbackValue='Text'}" />

Related

Difference between preview and running program

Sorry for my bad headline but I couldn't figure out a better one. Heck, I don't even know how to properly ask my question. But here it comes.
I have a custom control in WPF, let's call it Cell. This control does have a few dependency properties, one Text to show in the Cell and a few "decorative" properties for background color, foreground color and so on. If I use this Cell control stand-alone, everything works fine so far.
Then I have another custom control inheriting from ItemsControl, let's call it Field. This control should show a text and some Cells. it has some "decorative" properties for the text part as well. The information about showing Cells is given to the control by a DataTemplate, something like
<DataTemplate x:Key="CellTemplate"
DataType="...">
<ns:Cell Text="{Binding Text}"
Background="{Binding ...}" />
</DataTemplate>
...
<ns:Field ItemsSource="{Binding Cells}"
ItemTemplate="{StaticResource CellTemplate}" />
If I use this Field control stand-alone, everything works fine so far.
Now I want to show several Field controls at once. So I put an ItemsControl on my window and gave an ItemTemplate again, something like:
<DataTemplate x:Key="CellTemplate"
DataType="...">
<ns:Cell Text="{Binding Text}"
Background="{Binding ...}" />
</DataTemplate>
<DataTemplate x:Key="FieldTemplate"
DataType="...">
<ns:Field ItemsSource="{Binding Cells}"
ItemTemplate="{StaticResource CellTemplate}"
FieldText="{Binding Text}"
TextBackground="{Binding ...}" />
</DataTemplate>
<ItemsControl ItemsSource="{Binding Fields}"
ItemTemplate="{StaticResource FieldTemplate}" />
As long as I preview my WPF window everything works fine so far. Changing the values of some "decorative" properties at Cell level or at Field level is immediately shown in the preview.
But if I run my program it seems that all "decorative" properties at Cell level are ignored. I can see all my Fields with their respective texts and I can see every Cell in every Field with their respective texts. But all Cells are plain white. All set colors are not shown.
Snoop tells me, that every color is set to Transparent by the ParentTemplate.
Visual Studio doesn't show me any exceptions or any binding errors. So I'm kind of stuck at where or how I can find the error and fix it.
So I wanted to ask you, if you may have a hint for me.
Does this contruction with a DataTemplate containing a DataTemplate and both DataTemplates bind to it's DataContext work?
Or does it have something to do with maybe re-using Brush objects that shouldn't be re-used?
But why is it working in the preview?

WPF show small number beside all Controls

I have many FrameworkElements (TextBlock, CheckBox, ListBox..) and I would like to make something allowing me to show a small number besides every one control.
Some text ³
I came with the idea to write a MarkupExtension, where I could write that number like this:
..
<TextBlock Text="Some Text" SomeExtension="3" />
..
and then to add it somehow to the template of the Control.
But I'm sure, you guys have better solution for this problem ;)
One way to go with it would be create a Attached Property. Upon setting it on a control, a custom Adorner would be added for that control showing specified number.
Use the tag property to provide the number you want and inside the custom template databind to the property
<TextBlock Text="Some Text" Tag="3" />
and inside the controltemplate
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"/>

How can I add bold text and AccessText to a Label or TextBlock?

I have a WPF conundrum. I want some text to look like this:
Enter this preparer's info:
[ComboBox]
Alt+E is the access key that focuses the ComboBox, and when Alt is pressed, the E in the text should be underlined.
I can get the access key to work easily:
<Label Target="{Binding ElementName=PreparerComboBox}">
_Enter this preparer's info:</Label>
But then "preparer's" can't be bold because a Label doesn't support Runs (as far as I can tell).
I can do the bolding easily in a TextBlock:
<TextBlock>Enter this <Bold>preparer's</Bold> info:</TextBlock>
But there's no access key defined, so I tried adding my AccessText inside the TextBlock:
<Label Target="{Binding ElementName=PreparerComboBox}">
<TextBlock>
<AccessText>_Enter</AccessText> this <Bold>preparer's</Bold> info:
</TextBlock>
</Label>
But then the AccessText doesn't line up properly with the rest of the text in the TextBlock, and Margin doesn't seem to have any effect on it.
Example:
The best I've come up with so far is this monstrosity:
<Label Target="{Binding ElementName=PreparerComboBox}">
<WrapPanel>
<AccessText>_E</AccessText>
<TextBlock>nter this <Bold>preparer's</Bold> info:</TextBlock>
</WrapPanel>
</Label>
What am I missing here? Seems like there has to be an easier way.
Didn't change much but how about
<Label Target="{Binding ElementName=PreparerComboBox}">
<StackPanel Orientation="Horizontal">
<AccessText>_Enter</AccessText>
<TextBlock xml:space="preserve"> this <Bold>preparer's</Bold> info:</TextBlock>
</StackPanel>
</Label>

Bind value between control in different view -- WPF / MVVM

in my MVVM application (in wpf)
i have two view and I want to bind the context of my label on my textbox value (in the other view)
SelectorView.xaml contain this control:
<TextBox x:Name="tbArt" value="XX"/>
DescriptionView.xaml contain this control:
<label context="{binding on the tbArt value????}">
Is that possible directly without going in code behing and viewmodels ?
Will the label be refresh automatically ?
Thanks !
If both of the controls databinds to the same property the label will refresh when the value is changed.Make sure that the property triggers property changed when it gets changed.
ex:
in XAML.
<TextBox x:Name="tbArt" value="{Binding Path=TheProperty, UpdateSourceTrigger=PropertyChanged}"/>
<label context="{binding TheProperty}">
in the textbox make sure you use:
UpdateSourceTrigger=PropertyChanged
. otherwise the property won't be changed until focus is shifted from the textbox.
If all you want to do is display the value of one control in another control within the same window/page, you could do the following:
<TextBox x:Name="tbArt" Text="XX" />
<Label Content="{Binding Path=Text", ElementName=tbArt}" />
This will bind the content of the Text of the label to a control with the name "tbArt". You can do the same thing with other properties of the control as well.
For instance,
<TextBox x:Name="tbArt" Text="XX" Width=33 />
<Label Content="{Binding Path=Width, ElementName=tbArt}" />
will display "XX" in the textbox and "33" in the label.

WPF: How to embed button inside text flow (wrap text around button)?

I'd like an advice to the following problem: I want to embed a Button into a text flow, but when I embed a Button and Label (or TextBlock) into the WrapPanel, I get the first figure:
alt text http://sklad.tomaskafka.com/files/wpf-wrappanel-problem.png
I think that one of solutions could be FlowDocument, but I feel that this is far too heavy for a control simple like this (which could be used in several hundred instances). Do you have some other ideas about how to implement this? Thank you!
EDIT:
One solution could be the following (I didn't know it was possible to put more stuff into TextBlock), but I would lose the ability to bind (which I need):
<TextBlock TextWrapping="Wrap">
<Span>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<Run x:Name="MyLabel" Text="{Binding Path=Subject}" />
<!--
Problem: binding makes following error:
A 'Binding' cannot be set on the 'Text' property of type 'Run'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
-->
</Span>
</TextBlock>
To bind to Run.Text, checkout the BindableRun class by Fortes. Simple to implement, I use it all over my projects.
I found that implementing BindableRun correctly is pretty tricky - and almost all other available implementations will cause an exception from wpf layouting engine when the bound content changes from null to something non-null - see this problem, keyword "Collection was modified; enumeration operation may not execute."
Corrrect implementation from Microsoft is here - it shows how tricky this really is.
Solution: BindableRun class + the following markup:
<TextBlock>
<Button x:Name="MyButton" Command="{Binding Path=MyCommand}" Content="+" />
<common:BindableRun x:Name="Subject" BindableText="{Binding Path=Subject}"/>
</TextBlock>
Funny thing it works on the designer of a UserControl...
In that case, using the Property Change of your control to set the value to the Run is enough. I mean, if you had something like:
<TextBlock>
<Run Text="{Binding ElementName=thisCtrl, Path=Description}" />
</TextBlock>
Then just name the run, and on your property change handler of your UserControl DependencyProperty get/set the value.

Resources