Silverlight - Binding - silverlight

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;

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>

Dataform fields won't appear

I am trying to learn how to use the Silverlight 3 DataForm control, because I need to define the DataForm fields myself in the XAML code, that is, I don't want to use the AutoGenerateFields property.
My problem is: the dataform works perfectly when the AutoGenerateFields is set to true, but when I create a DataForm and set the fields manually and run the application, all I get is an empty blank rectangle where my form and its fields should be.
I created a blank Silverligh Navigation Application to test this, and below is the code of the Home.xaml page:
<Grid x:Name="LayoutRoot">
<StackPanel>
<!-- This doesn't work. It renders a blank rectangle -->
<dataFormToolkit:DataForm x:Name="DataForm">
<dataFormToolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel dataFormToolkit:DataField.IsFieldGroup="True">
<dataFormToolkit:DataField>
<TextBox Text="Test1" />
</dataFormToolkit:DataField>
<dataFormToolkit:DataField>
<TextBox Text="Test2" />
</dataFormToolkit:DataField>
<dataFormToolkit:DataField>
<TextBox Text="Test3" />
</dataFormToolkit:DataField>
</StackPanel>
</DataTemplate>
</dataFormToolkit:DataForm.EditTemplate>
</dataFormToolkit:DataForm>
<!-- This works. -->
<dataFormToolkit:DataForm x:Name="DataForm2"/>
</StackPanel>
</Grid>
To make the second DataForm work, I simply created a Person class, and put the following in Home.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Person client = new Person { Age = 10, DateOfBirth = new DateTime(1980, 10, 20), FirstName = "John", LastName = "Doe" };
DataForm2.CurrentItem = client;
}
You can see what happens when I run the application:
Does anyone know what's wrong? Thank you in advance.
To get something to appear I had to add:
DataForm.CurrentItem = client;
to the code.
This just displayed three text boxes with no labels and the entries "Test1", "Test2" and "Test3". Is this what you were expecting?
The Silverlight Toolkit samples page has an example of a template driven data form and it's XAML looks like this:
<dataform:DataForm x:Name="dataForm" ItemsSource="{Binding}" HorizontalAlignment="Left" MinWidth="400" MaxWidth="500" Margin="4" Grid.Column="1">
<dataform:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataform:DataField>
<TextBox Text="{Binding FirstName, Mode=TwoWay}" />
</dataform:DataField>
<dataform:DataField>
<TextBox Text="{Binding Email, Mode=TwoWay}" />
</dataform:DataField>
<dataform:DataField>
<TextBox Text="{Binding Phone, Mode=TwoWay}" />
</dataform:DataField>
<dataform:DataField Label="Calendar">
<controls:Calendar></controls:Calendar>
</dataform:DataField>
</StackPanel>
</DataTemplate>
</dataform:DataForm.EditTemplate>
</dataform:DataForm>
And there's the line:
DataContext = Contact.People;
in the code behind. (The class People is defined elsewhere as far as I can work out)
I also found it quite surprising that you need to bind to something before the form will even appear.
If you're trying to bind to a single item you need to do this :
CurrentItem="{Binding Customer}"
or - if you're in a user control, just
CurrentItem="{Binding}"
and then in the parent control
<my:AddressControl DataContext="{Binding Customer}"/>
Here's the full dataform:
<dt:DataForm Name="dfAddress" AutoGenerateFields="False" CurrentItem="{Binding}">
<dt:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dt:DataField Label="First Name">
<TextBox Text="{Binding FirstName, Mode=TwoWay}" Style="{StaticResource FieldTextBoxStyle}" HorizontalAlignment="Stretch" IsReadOnly="False" HorizontalContentAlignment="Stretch" />
</dt:DataField>
<dt:DataField Label="Last Name">
<TextBox Text="{Binding LastName, Mode=TwoWay}" Style="{StaticResource FieldTextBoxStyle}" HorizontalContentAlignment="Stretch" />
</dt:DataField>
</StackPanel>
</DataTemplate>
</dt:DataForm.EditTemplate>
</dt:DataForm>
You could give the CurrentItem="" in xaml. In this way, you dont need to actual bind it to anything and at the same time, work on the dataform looks :)

Having hardcoded text with a binding in a TextBlock

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}"/>

Resources