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>
Related
I have a simple Label in WPF as under:-
<Label Content="{Binding MyViewModel.SomeValue,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
I tried this but it didn't work:
<Label Canvas.Top="26" Canvas.Left="253" Content="{Binding "Hardcoded String"+CurrentRec.Current_Vendor_Purchase_Record.TaxName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
and this:
<Label Canvas.Top="26" Canvas.Left="253" Content="Hard Coded String Value"+ "{Binding CurrentRec.Current_Vendor_Purchase_Record.TaxName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
How can i concatenate some hard coded value to MyViewModel.SomeValue from XAML side?I mean i can always do it from code-behind,but just wanted to know how to add a hard coded value to a ViewModel property in XAML DataBinding?
I know it shouldn't be difficuilt,but plz discount me as a beginner :-).
You could use two different Run elements:
<Label Canvas.Top="26" Canvas.Left="253">
<Label.Content>
<TextBlock>
<Run Text="Hardcoded String" /><Run Text="{Binding CurrentRec.Current_Vendor_Purchase_Record.TaxName, Mode=OneWay}" />
</TextBlock>
</Label.Content>
</Label>
You don't event need to use a Label element:
<TextBlock Canvas.Top="26" Canvas.Left="253">
<Run>Hardcoded String</Run><Run Text="{Binding CurrentRec.Current_Vendor_Purchase_Record.TaxName, Mode=OneWay}" />
</TextBlock>
This will display the concatenated values in the view.
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?
I have information from different tables to bind on a Pop-up in a silverlight project.
My problem is that I can't bind those information because they are not in the same table.
how to bind the data contained in different tables in datagrid in silverlight appiliaction project.
Thank you
If you are using EntityFramework, and the data from different tables are related, you might be able to use the dotted notation to perform the binding. e.g.
<TextBlock Text="{Binding CustomerName}" />
<TextBlock Text="{Binding Address.City}" />
<TextBlock Text="{Binding Invoice.TotalAmount}" />
If the information is on separate objects, you can manually set the datacontext on each item separately. e.g.
<TextBlock x:Name="CustomerNameTextBlock" Text="{Binding CustomerName}" />
<TextBlock x:Name="CityTextBlock" Text="{Binding City}" />
<TextBlock x:Name="TotalAmountTextBlock" Text="{Binding TotalAmount}" />
this.CustomerNameTextBlock.DataContext = myCustomer;
this.CityTextBlock.DataContext = myAddress;
this.TotalAmountTextBlock.DataContext = myOrder;
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>
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}"/>