Having hardcoded text with a binding in a TextBlock - wpf

In WPF, is there any way to have the Text property of a TextBlock to contain both hard coded text and a specific binding?
What I have in mind is something along the lines of the following (ofcourse, the below doesn't compile):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>

There is, if you are on .Net 3.5 SP1
<TextBlock Text="{Binding Path=Artist.Fans.Count,
StringFormat='Number of Fans: {0}'}" />

In using the above approach:
<TextBlock Text="{Binding Path="Artist.Fans.Count,
StringFormat='Number of Fans: {0}'}" />
I found it somewhat restrictive in that I couldn't find a way to bold face inside the StringFormat nor could I use an apostrophe in the StringFormat.
Instead I went with this approach, which worked better for me:
<TextBlock TextWrapping="Wrap">
<Run>The value</Run>
<Run Text="{Binding Path=MyProperty1, Mode=OneWay}" FontWeight="Bold" />
<Run>was invalid. Please enter it with the format... </Run>
<LineBreak/><LineBreak/>
<Run>Here is another value in the program</Run>
<Run Text="{Binding Path=MyProperty2, Mode=OneWay}" FontWeight="Bold" />
</TextBlock>

Use Binding.StringFormat:
<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>

Here the binding value(clouds.all) is added with "%". You can add any value you want after "\{0\}".
<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>

With XAML using Template 10 and MVVM:
Just to be clear:
By definition, binding binds values to properties of controls.
Under the MVVM paradigm as implemented in the 'Template 10' framework, the values are initialized in the ViewModel associated to the relevant XAML page.
Here is how to have hardcoded text together with a binding in a Text property:
<Page
...
xmlns:vm="using:doubleirish.ViewModels"
xmlns:sys="using:System"
xmlns:controls="using:Template10.Controls"
...
<Page.DataContext>
<vm:StocksViewModel x:Name="ViewModel" />
</Page.DataContext>
...
<controls:PageHeader ... Text="{x:Bind sys:String.Format('Ticker : {0}', ViewModel.Ticker)}">
...
</Page>

The solution that worked for me:
<Label Content="{Binding Artist.Fans.Count}" ContentStringFormat="Number of {0}"/>

Related

Showing download percentage with xaml

I am trying to figure out a way to show the user the download progress like this:
17.38/50Mb's
But I need to bidnd through xaml with StringFormat
To use StringFormat in XAML,
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DownloadSizeInMB, StringFormat={0:0.00}}" />
<TextBlock Text="/" />
<TextBlock Text="{Binding TotalSizeInMB, StringFormat={0:0.00}}" />
<TextBlock Text="Mb's" />
</StackPanel>
But these comes with some Margin between the TextBlocks.
I would suggest you use MultiBinding instead.

Define ComboBox' DataTemplate for TextBox (and define search pattern) when IsEditable is True?

I have a ComboBox whose ItemsSource is binding to a collection and the SelectedItem is binding to the properties of my VieModel. Let's call the binding properties AvailableOptions and TargetOption in ViewModel. And type of collection item and TargetOption is called MyOption. I have such requirements but I don't know how to fulfill them all:
It should be OK for the binding TargetOption to have NULL as value
I want to set a DataTemplate for the target type in TargetOption collection to be displayed in the ComboBox
If possible, I want use different DataTemplate for MyOption when then are in the drop-down of ComboBox and when one item is selected. Because my UserControl has limited space so it should be compact when item is selected and during the selection it should provide more information
As I said, I don't know how to do all of them. At first I have the XAML like this:
<ComboBox SelectedItem="{Binding SelectedOption} ItemsSource="{Binding AvailableOptions}" >
<ComboBox.ItemTemplateSelector>
<MyNameSpace:ComboBoxItemTemplateSelector ItemTemplate="{StaticResource OptionDetailTemplate}" SelectedItemTemplate="{StaticResource OptionSimpleTemplate}" />
</ComboBox.ItemTemplateSelector>
</ComboBox>
With a customized ItemTemplateSelector. I am able to do the requirement 2) and 3). My OptionDetailTemplate looks like this:
<DataTemplate x:Key="OptionDetailTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ShortName}" />
<TextBlock Text=" | " />
<TextBlock Text="{Binding Code}" />
</StackPanel>
</DataTemplate>
and OptionSimpleTemplate looks like this:
<DataTemplate x:Key="OptionSimpleTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ShortName}" />
<TextBlock Text=" | " />
<TextBlock Text="{Binding Code}" />
<TextBlock Text=" | " />
<TextBlock Text="{Binding Number}" />
</StackPanel>
</DataTemplate>
But now the problem is requirement 1). When the user select one option from the drop down list of the ComboBox, he can't put it as NULL back which should be allowed. This is because AvailableOption doesn't have a NULL object
I see that if I set IsEditable to True for the ComboBox, and set TextSearch.TextPath to Code, it allows the text quick search/assign and also allows to have a NULL value when the search text is totally deleted. But now when I ever select one, it only displays Code (the OptionTemplate does not have any effect any more because now it displays the selected item in a TextBox). This is not good since only Code is not enough for the user to tell what Option it is. But since I have multiple properties in MyOption class, how can I define the DataTemplate for the TextBox and also define the search routine?
I have to be honest that I didn't fully understand your first requirement and its ramifications. however, I am really just answering to let you know that you don't even need to use a DataTemplateSelector to select between your two DataTemplates. If you do not set the x:Key value on them, then they will be applied to the relevant items implicitly:
<DataTemplate DataType="{x:Type YourXamlNamespacePrefix:TargetOption}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ShortName}" />
<TextBlock Text=" | " />
<TextBlock Text="{Binding Code}" />
</StackPanel>
</DataTemplate>
...
<DataTemplate DataType="{x:Type YourXamlNamespacePrefix:MyOption}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ShortName}" />
<TextBlock Text=" | " />
<TextBlock Text="{Binding Code}" />
<TextBlock Text=" | " />
<TextBlock Text="{Binding Number}" />
</StackPanel>
</DataTemplate>
Furthermore, you could do all this data binding with just a single TextBlock if you use a MultiDataTrigger:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}|{1}">
<Binding Path="ShortName" />
<Binding Path="Code" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Perhaps if you try clarifying your remaining problem (in your question), I might understand?

Data Binding Data Formatting Issue

I have a AutoCompleteBox bound to the datasource. DataSource contains two string
properties. I have defined ItemTemplate for AutoCompleteBox.
I want second property to come closed in brackets e.g. Property1 Data (Property2 Data)
I will have to define StringFormat during Binding.
I am totally unaware of format. Can anyone tell me the format.
Thanks.
here is examples.
A simple way to use StringFormat in the binding.
Output: (0)
<TextBlock Text="{Binding Videos.Count, StringFormat='({0})', FallbackValue='(0)'}" />
Using <Run> tags you can also build complex values.
Output: Distance: 200km
<TextBlock>
<Run Text="Distance: " />
<Run Text="{Binding VideoDistance, StringFormat='\{0:G\}'}" />
</TextBlock>
If you use the second example, you must just add another <Run> tag for the next value.
You can also use a horizontal StackPanel to show multiple values.
Output: Start Distance: 200km
<!--START DISTANCE MIN-->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Start Distance:" />
<TextBox Text="{Binding StartDistanceMinStr, Mode=OneWay}" IsReadOnly="True" />
</StackPanel>

Binding to DevExpress GridControl TotalSummary

I have a basic newbie binding question, which isn't necessarily directly related to the control used. Anyway, here's the thing: I have a DXGrid with a TotalSummary defined, which counts the rows of the grid.
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem x:Name="grdCompleteCount" FieldName="ar_id" SummaryType="Count"/>
</dxg:GridControl.TotalSummary>
Now I'd like to display the count not at the bottom of the grid as it is done automatically, but would like to bind it to another element, say, a textblock. Something like this:
<TextBlock x:Name="statusBarGridCount"
Text="{Binding ElementName=grdCompleteCount, Path=Value}"
TextAlignment="Right"
Width="190" />
But this approach doesn't work as I'm not sure how to get to the value I'm looking for. What's wrong with the binding?
Ok, got it already, don't worry... :) Works that way:
<StatusBarItem Grid.Column="2" BorderThickness="1" Margin="1">
<TextBlock x:Name="statusBarGridCount"
Text="{Binding ElementName=grdList, Path=VisibleRowCount}"
TextAlignment="Right"
/>
</StatusBarItem>

Silverlight for Windows phone 7 - two text blocks after each other with NO linebreak

I'm relatively new to Windows Phone 7 development, so bear with me on this.
I've two text blocks, each with binding to two different properties, like this:
<TextBlock Text="{Binding ID}" />
<TextBlock Text="{Binding Title}"/>
So when I deploy this code, on the phone it will be something like this:
6
This is a title
I want the text to be displayed this way instead:
6: This is a title
What is the easiest way to achieve this?
<TextBlock>
<Run Text="{Binding ID}" />:
<Run Text="{Binding Title}"/>
</TextBlock>
Three most common UI layout elements are StackPanel, Grid & Canvas. Here's one way..
<StackPanel Orientation="Horizontal">
<TextBlock .. />
<TextBlock .. />
</StackPanel>

Resources