Why is binding result different on a Label and a TextBox? - wpf

I was using the following XAML:
<Label Grid.Row="0" Grid.Column="0" Content="Datum"/>
<Label Grid.Row="0" Grid.Column="1" Content="{Binding TimeStamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<Label Grid.Row="0" Grid.Column="2" Content="Level"/>
<Label Grid.Row="0" Grid.Column="3" Content="{Binding Level}"/>
but the TimeStamp was being formatted like this:
2.24.2012 7:38
I started up Snoop (great tool!) and noticed that the Label is actually composed of a TextBox and that this TextBox contained the TimeStamp formatted as I defined it. I then replaced the Label with a TextBox and I get the TimeStamp correctly formatted.
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding TimeStamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
2012-02-24 07:38:23.123
I have defined no Resource, Trigger or Style blocks to override Label behaviour so I'm wondering why this is happening.
Any ideas?

The Binding.StringFormat property doesn't work on Labels, you need to use the ContentStringFormat property on the Label
<Label Grid.Row="0" Grid.Column="1" Content="{Binding TimeStamp}">
<Label.ContentStringFormat>0:yyyy-MM-dd HH:mm:ss.fff</Label.ContentStringFormat>
</Label>
also see Binding only part of a label

Related

WPF/CaliburnMicro Bind one property to two form controls

I have one PersonModel object, which has two properties, FirstName and LastName. I created a CurrentPerson property of type PersonModel in my ViewModel. When binding to the controls, whichever is bound to x:Name is the only one that shows up at runtime
TextBlock Displays FirstName
<TextBlock x:Name="CurrentPerson_FirstName" Grid.Column="0" Grid.Row="0"></TextBlock>
<TextBox Text="{Binding Path=CurrentPerson_FirstName}" Grid.Column="0" Grid.Row="1"/>
TextBox Displays FirstName
<TextBlock Text="{Binding Path=CurrentPerson_FirstName}" Grid.Column="0" Grid.Row="0" ></TextBlock>
<TextBox Name="CurrentPerson_FirstName" Grid.Column="0" Grid.Row="1" />
How can I have both the TextBlock and the TextBox display the same data, and if the TextBox is typed in, the TextBlock and CurrentPerson will be updated?
When not using x:Name, you should stick to the conventional naming pattern of using ".". In your TextBlock, you need to replace "CurrentPerson_FirstName" with "CurrentPerson.FirstName".
For example,
<TextBlock Text="{Binding Path=CurrentPerson.FirstName}" Grid.Column="0" Grid.Row="0" ></TextBlock>
This should help you bind both control to same property.

Textbox Highlighting Always Starting at 0

This is really annoying.
I have basic textboxes which do not allow highlighting of single words/characters. Every time I try to select single words/characters, the highlight immediately jumps to the front of the box and highlights everything up to where I clicked.
<TextBox Name="txtTaskName" AutoWordSelection="False" Grid.Row="0" Grid.Column="2" Text="{Binding TaskName, Mode=TwoWay}" MaxWidth="300" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" TextWrapping="Wrap" VerticalContentAlignment="Center"></TextBox>
Suggestions?
Set AutoWordSelection="False" on your TextBox
<TextBox AutoWordSelection="False"/>

Calendar control width is too large at initialization of usercontrol

I have used calendar control and its display mode is year when my user control is loads then calender width s too large and doesn't appear when width is set .but when is select any month then width is set as it should
i m not getting what is the issue behind it
can anyone help me quickly???
Here is my code and what ever selected month will appear in textbox's text property and on click event of button my calendar control is set to visible
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="HorizontalHorizontalAlignment="Left "Margin="4">
<Label Content="Month: " HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" FontWeight="DemiBold" Margin="4"/>
<TextBox Height="23" HorizontalAlignment="Stretch" Name="SelectedMonthTextBox" VerticalAlignment="Top" Width="130" Margin="4" LostFocus="SelectedMonthTextBox_LostFocus" SelectionBrush="#FF767879"/>
<Button Content=".." Height="20" Width="20" Margin="4" Name="button" Click="button_Click" />
</StackPanel>
<Calendar Grid.Row="1" Grid.Column="0" Margin="50,-8" Panel.ZIndex="100" Name="calendar1" DisplayModeChanged="calendar1_DisplayModeChanged" DisplayDateChanged="calendar1_DisplayDateChanged" HorizontalAlignment="Left" DisplayMode="Year" Style="{StaticResource CalendarStyle}" Focusable="False" HorizontalContentAlignment="Left" VerticalAlignment="Top" />
i took a look your code and from information you provided so far, there is no any change a you describe. I believe that you should check following:
What happens in calendar1_DisplayModeChanged
How defined your style: StaticResource CalendarStyle
If it possible please post them here. I will try to help as soon as possible :)

Does StringFormat feature of WPF Xaml work on Label.Content?

I have bind my Amount Label's Content Property to a decimal property via DataContext. I am trying to apply stringformat but see no effect. Does StringFormat feature work on Label controls ?? Please tell me on which controls does this feature work. BTW following is the code for the Label Control for whom i want to apply the currency formatting
<Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" />
StringFormat works on properties of type string (when the object you are binding to is being converted to a string the string format is applied). The Content property is of type Object.
You can place a TextBlock inside your label to achieve the desired effect:
<Label Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120">
<TextBlock Text="{Binding Path=Amount, StringFormat={}{0:C}}"/>
</Label>
Try ContentStringFormat
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/866f7934-8b10-4872-b306-122674fad5fa/
<Label Content=ā€{Binding Amount}ā€ ContentStringFormat=ā€Cā€ />

Label Target="{Binding ElementName=UIName}" bug, or is it just me?

I've been tring to get this example to work, but there seems to be a bug in the code Label Target code, in that when you click on the second label the focus is set to the DatePicker rather than the TextBox, regardless which label you click first, also when you have entered the date the second label still sets it focus to DatePicker.
<!-- Unbound Date of Birth field -->
<sdk:Label Content="Date of Birth" IsRequired="True" Margin="5" />
<StackPanel Orientation="Horizontal" Grid.Column="1">
<sdk:DatePicker Height="23" />
<sdk:DescriptionViewer Description="Please enter your date of birth."/>
</StackPanel>
<!-- ID Number field -->
<sdk:Label Grid.Row="1" Margin="5" Target="{Binding ElementName=tbIdNumber}" />
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<TextBox x:Name="tbIdNumber" Height="23" Width="100"
Text="{Binding IdNumber, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<sdk:DescriptionViewer Target="{Binding ElementName=tbIdNumber}"/>
</StackPanel>
I found this example here on the MSDN link text. To me this seems to be a bug, or am I just losing it?
Apologies if this is a duplicate question... I have googled this and all the examples seem to be the same, in that the target element is not being focused to.
It seems a reasonable assumption that clicking the label ought to move the focus to the associated control. However the Label does not actually provide that function.

Resources