display computed property in silverlight - wpf

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>

Related

Why is this Binding not working in XAML?

I'm using this XAML to simulate watermark text on a textbox, but the binding for the Text Property is not working:
<TextBox x:Name="txtSearch" BorderThickness="0">
<ap:CueBannerService.CueBanner>
<TextBlock Foreground="Black" Opacity=".7" Text="{Binding Path=WatermarkText}"/>
</ap:CueBannerService.CueBanner>
</TextBox>
On the other hand, any of the follow code works:
<TextBlock Foreground="Black" Opacity=".7" Text="Watermark Test"/>
<TextBox Opacity=".7" Text="{Binding Path=WatermarkText}"/>
Why is not working? thank you!
UPDATE
This also works fine (without the TextBlock):
<TextBox x:Name="txtSearch" BorderThickness="0" ap:CueBannerService.CueBanner="{Binding Path=WatermarkText}">
UPDATE 2
This Also works!:
<TextBox x:Name="txtSearch" BorderThickness="0">
<ap:CueBannerService.CueBanner>
<TextBlock Foreground="Black" Opacity=".7" Text="Watermark Text"/>
</ap:CueBannerService.CueBanner>
</TextBox>
Replace the binding for TextBlock with the following:
{Binding DataContext.WatermarkText, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}
Will check why this did not work.
This should work! We are ensuring that the DataContext that is assosciated with the TextBox is being used for the TextBlock:
{Binding DataContext.WatermarkText, ElementName=txtSearch}

Commanding with Static Items of Listbox using MVVM

I have a list box in my view with three static items(Name,Age,Gender), and I want my ViewModel to do something when an item in the ListBox is selected. I want to do this without any code-behind, using the MVVM pattern.
My goal is to navigate to a page when an item is selected also the items stated above does not come from an observable list, it is hardcoded in my XAML. How would I do that? Please teach me. If you don't mind to send a sample, that would be a great help. Thanks much in advance.
<ListBox x:Name="lbviewlist">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged" >
<Command:EventToCommand Command ="{Binding ViewCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.Items>
<StackPanel x:Name="lbiview1" Orientation="Vertical">
<ListBoxItem Content="Name" FontSize="35" Margin="10,0,0,0"
Foreground="OrangeRed"/>
<TextBlock TextWrapping="Wrap" Text="View name of the patient"
FontSize="25" Margin="10,0,0,0" Foreground="White"/>
</StackPanel>
<StackPanel x:Name="lbiview2" Orientation="Vertical">
<ListBoxItem Content="Age" FontSize="35" Margin="10,20,0,0"
Foreground="OrangeRed"/>
<TextBlock TextWrapping="Wrap" Text="View age of the patient"
FontSize="25" Margin="10,0,0,0" Foreground="White"/>
</StackPanel>
<StackPanel x:Name="lbiview3" Orientation="Vertical">
<ListBoxItem Content="Gender" FontSize="35" Margin="10,20,0,0"
Foreground="OrangeRed"/>
<TextBlock TextWrapping="Wrap" Text="View the gender of the patient"
FontSize="25" Margin="10,0,0,0" Foreground="White"/>
</StackPanel>
</ListBox.Items>
</ListBox>
You can use data binding to bind the SelectedItem of the ListBox to a property on your view model. Then, in the setter of your view model property, you can run the logic you need to when the selected item changes.
<ListBox SelectedItem="{Binding MySelectedItem}" ... />
Update
Ok, firstly I would strongly recommend (no insist) that you use an MVVM framework. If you're doing MVVM, then you need to use a framework.
Next, there's no reason why these list items can't be on your view model. It would certainly make your view a lot cleaner.
Then you can set your ListBox ItemsSource to bind to your view model collection, and then use data templating in your view to render each item in the list consistently.
E.g. your view can end up something like:
<ListBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Value}" ... />
<TextBlock Text="{Binding Description}" ... />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Much cleaner, I think you'll agree.

WPF MVVM customizing ListView cell to insert hyperlink and change text color

I have a GridView cell in a ListView that is defined as a TextBlock and bound to a string on my ViewModel. I want to be able to change parts of the text into hyperlinks and part into different colors programatically.
Here is the XAML for the GridView cell:
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="#dfdfdf" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Data}" Margin="3"/>
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
And here's what it looks like at the moment:
An example of the text that is bound would be:
<color:#ff0000>Test item</color>
Test item 2
<link:http://www.google.com>Test hyperlink</link>
I have no problem with the regex to parse the bound text and pull out the required information but how would I go about changing the TextBlock into different colors and add a hyperlink?
Thanks in advance
You can put multiple Run elements inside a TextBlock and style then however you like.
Here is an example with a working hyperlink which even supports MVVM :)
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="#dfdfdf" HorizontalAlignment="Stretch">
<TextBlock Margin="3">
<Run Text="{Binding Data}" />
<Run Text="Some more data" Background="Red" />
<Run Text="Click Me" Foreground="Blue" TextDecorations="Underline" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<local:EventToCommand Command="{Binding LinkClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Run>
</TextBlock>
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
Note:
The Interaction.Triggers are from System.Windows.Interactivity and the EventToCommand from MVVMLight.
Similar to Blachshma's answer, but using a hyperlink right in there with the Run objects:
<TextBlock>
<Run Text="{Binding Data}" />
<Run Text="Some more data" Background="Red" />
<Hyperlink Command="{Binding Path=Command}">
<TextBlock Text="{Binding Path=Text}"/>
</Hyperlink>
</TextBlock>
Clearly I have a Command and Text properties to bind to for the hyperlink. Will look exactly the same, but will also respond to the command's CanExecute predicate (hyperlinks go grey).

Telerik RadComboBox not showing Selected Item

I have a RadComboBox that i have bound like shown below
<telerik1:RadComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" Margin="5,2" ItemsSource="{Binding RepTypes}" DisplayMemberPath="Path=TypeName" SelectedValuePath="Value" SelectedItem="{Binding RepType, Mode=TwoWay}" >
</telerik1:RadComboBox>
When i select an Item I catch the Property Changed event, but basically the selection in the combo box stays blank.
What am i doing wrong?
Ok i made it so that it shows up now.. But i don't understand why... Or how to change it so it works for me in all cases...
<telerik1:RadComboBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" Margin="5,2" ItemsSource="{Binding RepTypes}" SelectedValuePath="Value" SelectedItem="{Binding RepType, Mode=TwoWay}" >
</telerik1:RadComboBox>
Thats what works... the Biggest difference was. I had to name a field to "Name" and then bind it and take out the DisplayMemberPath="Path=ReportName"
If that is the case then how do i tell the control what Field to Display in the dropdown?
Are you somehow changing your collection? The controls only look for the items once. So, if the page loads and then you're loading your collection of RepTypes, it doesn't update the dictionary. I'm doing something similar and I'm lazy loading my collection (as you type, I get more from the database).
<t:RadComboBox x:Name="RepTypeComboBox" Margin="0,1"
t:TextSearch.TextPath="TypeName"
ItemsSource="{Binding Path=RepTypes, Mode=OneWay}"
SelectedValue="{Binding Path=Reptype, Mode=TwoWay, NotifyOnValidationError=True}"
IsEditable="True"
Grid.Column="1"
Grid.Row="2" TabIndex="1">
<t:RadComboBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock FontWeight="Bold" Text="{Binding Path=TypeName, Mode=OneWay}" Width="75"/>
<TextBlock Text=": " />
<TextBlock Text="{Binding Path=address1, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Path=address2, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Path=citystate, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Path=zip, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</t:RadComboBox.ItemTemplate>
</t:RadComboBox>
If you want ReportName to be shown as your display member, you only have to put it this way:
<telerik1:RadComboBox ItemsSource="{Binding RepTypes}" SelectedValuePath="Value"
SelectedItem="{Binding RepType, Mode=TwoWay}" DisplayMemberPath="ReportName">
</telerik1:RadComboBox>
You're putting an extra "Path=" that's only confusing the XAML parser.

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

Resources