Binding in Label.ContentTemplate - wpf

In the Xaml below, the first control (the TextBlock by itself) has no problem binding and rendering the value of RecordCount. But in the second control (the Label with the ContentTemplate), the value of RecordCount is not rendered. However, the literal "Cars" is rendered fine. So I know the ContentTemplate is working, but the binding to RecordCount from within the ContentTemplate is not. What am I doing wrong?
<TextBlock Text="{Binding RecordCount}"/>
<Label HorizontalAlignment="Center" >
<Label.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding RecordCount}"/>
<TextBlock Text=" Cars"/>
</StackPanel>
</DataTemplate>
</Label.ContentTemplate>
</Label>

Set the Content property on the Label to the current DataContext:
<Label HorizontalAlignment="Center" Content="{Binding}">
or, set the StackPanel as the Content and don't use a template at all:
<Label HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding RecordCount}"/>
<TextBlock Text=" Cars"/>
</StackPanel>
</Label>
The ContentTemplate is used to present the Content. Since it is null, the DataContext is null when your template is instantiated. The TextBlocks are still created, so Cars is rendered, but null doesn't have a RecordCount property so the first text block is rendered with no text.
Also, if you are only using two TextBlocks to concatenate text, you can use the StringFormat property in .NET 3.5 SP1 or later:
<Label Content="{Binding RecordCount}" ContentStringFormat="{}{0} Cars"/>

Just an alternative, you can bind the RecordCount to Label's Content and have DataTemplate takes care on how you are going to show it.
<Label HorizontalAlignment="Center" Content="{Binding RecordCount}" >
<Label.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding}"/>
<TextBlock Text=" Cars"/>
</StackPanel>
</DataTemplate>
</Label.ContentTemplate>
</Label>

Related

display computed property in silverlight

I want to display first & last name of person in my form (xaml)
I use binding for getting person property from screen but I don't have computed property in screen because I don't know how can I create this one!!
In that code I use only firstname but I want first and last name!!! :(
<telerik:Label Grid.Row="4" Grid.Column="0" Content="{Binding PersonelPropertiesTab,Source={StaticResource localResource}}"/>
<telerik:RadComboBox Grid.Row="4" Grid.Column="1" ItemsSource="{Binding Screen.PersonelProperties}"
SelectedItem="{Binding Screen.CurriculuminformationProperty.PersonelProperty,Mode=TwoWay}"
SelectedValue="Id" DisplayMemberPath="applicantfirstname"/>
<viewer:DescriptionViewer Grid.Row="4" Grid.Column="5" Description="{Binding PersonelPropertiesTab,Source={StaticResource localResource}}"/>
I read some article about thatthey are for lightswitch not silverlight.
Could you give me good references?
Instead of setting the DisplayMemberPath, you may set a DataTemplate for the ComboBox items. I haven't tested that with a RadComboBox, but I assume that it behaves like a standard ComboBox:
<telerik:RadComboBox ...>
<telerik:RadComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Inlines>
<Run Text="{Binding FirstName}"/>
<Run Text=" "/>
<Run Text="{Binding LastName}"/>
</TextBlock.Inlines>
</TextBlock>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
</telerik:RadComboBox>

Silverlight textblock in a listbox not wrapping

I have a data bound Listbox as shown below. I want the textblock that holds the data to wrap. And I have not been able to.
What is the problem here?
Here is my code:
<DataTemplate x:Key="policyLbTemplate">
<StackPanel>
<TextBlock Text="{Binding name}" FontWeight="Bold"/>
<TextBlock Text="{Binding description}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
<ListBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Policies}"
ItemTemplate="{StaticResource policyLbTemplate}"
HorizontalContentAlignment="Stretch" />
You need to add one property to the list box: ScrollViewer.HorizontalScrollBarVisibility="Disabled", then the text will wrap (otherwise it grows offscreen). I have done it in the following code and it works fine for me.
<ListBox Name="lstbIcons" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding UserActionName}" FontWeight="Bold"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is most likely because there is nothing restricting the width of the TextBlock so that it is growing offscreen. Do Horizontal scrollbars appear?
See the following related questions and try the solutions described:
WP7 TextBlock inside a ListBox not wrapping text
Force TextBlock to wrap in WPF ListBox
windows phone 7 TextBlock TextWrapping not honored in listbox
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/37236ac6-05c3-4acc-baca-abc871ba64e0

Binding Between elements inside DataTemplate

How can I make binding between properties of two elements inside dataTemplate ?
example :
// Bind Text property to Content property
<DataTemplate>
<TextBox Text="{Binding}" />
<Label Content="{Binding}" />
</Datatemplate>
If I correctly understood.
<DataTemplate>
<TextBox x:Name="p_text" Text="{Binding login}"/>
<Button Content="{Binding ElementName=p_text, Path Text}"/>
</DataTemplate>

WPF Binding question

I have a question regarding the binding
I have two textbox, textbox one is bind to the property NAME and has the tag FULLName
< TextBox Name="NewTextBox" Tag="FullName" Text="{Binding Path = Name}" >
In the second textbox I need to bind to the property that is available as the tag on the first textbox.
How to do it in XAML?
<TextBox Name="NewTextBox" Tag="FullName" Text="{Binding Path=Name}" />
<TextBox x:Name="NewTextBox2" Text="{Binding ElementName=NewTextBox, Path=Tag}"/>
I'm not sure but is this what you're looking for?
<TextBox x:Name="textbox1" Text="{Binding Path=Name}" Tag="{Binding Path=FullName}"/>
<TextBox x:Name="textbox2" Text="{Binding ElementName=textbox1, Path=Tag}"/>

Vertical alignment of WPF Checkbox content with respect to checkbox

I have a checkbox whose XAML markup is as follows:
<CheckBox HorizontalContentAlignment="Stretch">
<StackPanel Orientation="Vertical">
<TextBox x:Name="editTextBox"
Text="{Binding Path=Caption, Mode=TwoWay}"
TextWrapping="Wrap"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource booleanToVisibilityConverter}}" />
<TextBlock x:Name="itemText"
TextWrapping="Wrap"
Text="{Binding Path=Caption}"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource reverseBooleanToVisibilityConverter}}">
</TextBlock>
</StackPanel>
</CheckBox>
The idea is that I can switch between the TextBlock (display) and TextBox (edit). However, when running the application, the CheckBox visual (the checkable square) is vertically centered. I would like it to be aligned with the top of the TextBlock/TextBox instead.
I've noticed that when I only include a TextBlock (so no StackPanel, no TextBox), the checkbox is in fact aligned with the top of the TextBlock, so I'm assuming I'm missing some setting on the StackPanel?
First of all don't waste your time with VerticalAlignment or VerticalContentAlignment (or even ControlTemplate). They're not going to do what you want or might expect.
As described on MSDN a BulletDecorator (which is the control that CheckBox and RadioButton uses to render a radio/check button) will set the position of the icon automatically. You have no additional control over this:
A Bullet always aligns with the first line of text when the Child
object is a text object. If the Child object is not a text object, the
Bullet aligns to the center of the Child object.
Unless you change the control template (unnecessary) you'll only be able to position the radio/check icon at the top if the content is text.
So if you do something like this it won't look good because you won't be able to move the icon no matter how many VerticalAlignment properties you try to set.
<RadioButton>
<StackPanel>
<TextBlock Text="First line"/>
<TextBlock Text="Something else"/>
</StackPanel>
</RadioButton>
BUT fortunately you can put pretty much anything you want in a TextBlock using InlineUIContainer. The text (or content) in the first line will dictate the position of the icon automatically. If you want something underneath the first line that isn't text, just use <Linebreak/> and then <InlineUIContainer/>
Here's an example that has an oversized TextBox to show more clearly what's happening.
<RadioButton>
<TextBlock VerticalAlignment="Top" TextWrapping="Wrap">
<TextBlock Text="Products with <" VerticalAlignment="Center" Margin="0,0,5,0"/>
<InlineUIContainer BaselineAlignment="Center">
<TextBox FontSize="30" Width="25" Text="10" Margin="0,0,5,0"/>
</InlineUIContainer>
<TextBlock VerticalAlignment="Center" Margin="0,0,5,0">
<Run Text="days" FontWeight="Bold"/>
<Run Text="inventory" />
</TextBlock>
<LineBreak/>
<InlineUIContainer>
<StackPanel>
<CheckBox Content="Include unsold products" />
<CheckBox Content="Include something else" />
</StackPanel>
</InlineUIContainer>
</TextBlock>
</RadioButton>
CheckBox.Content doesn't give you very much control over how things are displayed. Try this instead:
<DockPanel VerticalAlignment="Top"> <!-- can also be center or bottom -->
<CheckBox/>
<Grid>
<TextBox x:Name="editTextBox"
Text="{Binding Path=Caption, Mode=TwoWay}"
TextWrapping="Wrap"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource booleanToVisibilityConverter}}" />
<TextBlock x:Name="itemText"
TextWrapping="Wrap"
Text="{Binding Path=Caption}"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource reverseBooleanToVisibilityConverter}}">
</Grid>
</DockPanel>

Resources