WPF Binding question - wpf

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

Related

How to Bind with a StringFormat

I have a ComboBox which I am binding to an IEnumerable<int> source.
The source has values like 12,13,14 but I want the ComboBox to display Version 12, Version 13, Version 14 etc with SelectedValue still 12, 13 and 14.
For now I am modifying the Source to add Version to it and then Binding the ComboBox to an IEnumerable.
XAML
<ComboBox x:Name="ComboBoxVersions"
SelectedIndex="0"
SelectionChanged="ComboBoxVersions_OnSelectionChanged"
ItemsSource="{Binding EnvironmentVersions}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You could use something like this
<TextBlock Text="{Binding StringFormat=Version: {0}}" />
using ComboBox.ItemStringFormat:
<ComboBox ItemsSource="{Binding EnvironmentVersions}"
ItemStringFormat="version: {0}" />
or using ComboBox.ItemTemplate
<DataTemplate>
<TextBlock Text="{Binding StringFormat=Version: {0}}" />
</DataTemplate>
or
<DataTemplate>
<TextBlock>
<Run Text="Version " />
<Run Text="{Binding }"/>
</TextBlock>
</DataTemplate>
Here's a simple way:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Version " />
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
Since the ItemTemplate only defines how the items are displayed, the SelectedItem property of the ComboBox still holds the original value from your collection of version numbers.
You can add a string format to the binding in the datatemplate.
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringFormat=Version {0}}" />
</DataTemplate>
</ComboBox.ItemTemplate>

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}

display computed property in silverlight

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>

Binding in Label.ContentTemplate

In the Xaml below, the first control (the TextBlock by itself) has no problem binding and rendering the value of RecordCount. But in the second control (the Label with the ContentTemplate), the value of RecordCount is not rendered. However, the literal "Cars" is rendered fine. So I know the ContentTemplate is working, but the binding to RecordCount from within the ContentTemplate is not. What am I doing wrong?
<TextBlock Text="{Binding RecordCount}"/>
<Label HorizontalAlignment="Center" >
<Label.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding RecordCount}"/>
<TextBlock Text=" Cars"/>
</StackPanel>
</DataTemplate>
</Label.ContentTemplate>
</Label>
Set the Content property on the Label to the current DataContext:
<Label HorizontalAlignment="Center" Content="{Binding}">
or, set the StackPanel as the Content and don't use a template at all:
<Label HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding RecordCount}"/>
<TextBlock Text=" Cars"/>
</StackPanel>
</Label>
The ContentTemplate is used to present the Content. Since it is null, the DataContext is null when your template is instantiated. The TextBlocks are still created, so Cars is rendered, but null doesn't have a RecordCount property so the first text block is rendered with no text.
Also, if you are only using two TextBlocks to concatenate text, you can use the StringFormat property in .NET 3.5 SP1 or later:
<Label Content="{Binding RecordCount}" ContentStringFormat="{}{0} Cars"/>
Just an alternative, you can bind the RecordCount to Label's Content and have DataTemplate takes care on how you are going to show it.
<Label HorizontalAlignment="Center" Content="{Binding RecordCount}" >
<Label.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding}"/>
<TextBlock Text=" Cars"/>
</StackPanel>
</DataTemplate>
</Label.ContentTemplate>
</Label>

Syntax to bind to the current dataitem's default indexer in WPF?

I have a Listbox with an itemssource set to an ObservableCollection of DataRow. Let's say each DataRow has 5 columns for this example.
In the DataTemplate of the ListBox I have 5 textblocks (1 for each column). My question is how can I bind to an indexer of the row to get the columns value?
Here is my attempt but nothing displays so I must have the syntax wrong:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=.[0]}" />
<TextBlock Text="{Binding Path=.[1]}" />
<TextBlock Text="{Binding Path=.[2]}" />
<TextBlock Text="{Binding Path=.[3]}" />
<TextBlock Text="{Binding Path=.[4]}" />
</StackPanel>
</DataTemplate>
I know that indexers can be used in bindings because I've done something like this already:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Collection[0].Name}" />
<TextBlock Text="{Binding Path=Collection[1].Name}" />
<TextBlock Text="{Binding Path=Collection[2].Name}" />
<TextBlock Text="{Binding Path=Collection[3].Name}" />
<TextBlock Text="{Binding Path=Collection[4].Name}" />
</StackPanel>
</DataTemplate>
Any help on correcting my syntax would be appreciated.
Your syntax should do. However, the following must go also:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=[0]} />
<TextBlock Text="{Binding Path=[1]} />
<TextBlock Text="{Binding Path=[2]} />
<TextBlock Text="{Binding Path=[3]} />
<TextBlock Text="{Binding Path=[4]} />
</StackPanel>
</DataTemplate>
Check your ItemsSource. Does it really provide an indexer that is of type int? Maybe you have an indexer of another type or even no index. Maybe it's another object than you expect it to be?

Resources