Why is this Binding not working in XAML? - wpf

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}

Related

How to specify Binding XPath using property element syntax?

I need to apply an XPath Binding for ItemsSource of an HierarchicalDataTemplate. The XPath binding is working correct when applied using property attribute syntax, but in a case, I have very long XPath expression to use and so I thought to apply it using property element syntax.
This is what is working using the Property Attribute Syntax:
<HierarchicalDataTemplate
x:Key="rootTemplate"
ItemsSource="{Binding XPath=./*}">
<StackPanel
Orientation="Horizontal"
VerticalAlignment="Center">
<TextBlock
Margin="5,0,0,0"
Text="{Binding Name}" />
<Image
Source="repeat.ico"
Margin="5 0 0 0"
Width="20"
Height="20"
Visibility="{Binding Converter={StaticResource RepeatToVisiblityConverter}}"></Image>
</StackPanel>
</HierarchicalDataTemplate>
And this is what I am trying to achieve for the same template above, using the Property Element syntax when using the long XPath: [Although I have used the same XPath here in question, but in real it is long]
<HierarchicalDataTemplate
x:Key="rootTemplate">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.XPath>
<![CDATA[./*]]>
</Binding.XPath>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel
Orientation="Horizontal"
VerticalAlignment="Center">
<CheckBox
Margin="5,0,0,0" />
<TextBlock
Margin="5,0,0,0"
Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
The ItemsSource becomes empty and nothing is shown when using this syntax for specifying the Binding.
Does anyone know, how to fix this?
The following code works:
It just removes these two lines from above:
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
I will find out later, why removing this works.
<HierarchicalDataTemplate
x:Key="rootTemplate">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.XPath>
./*[#Repeat="true" or .//*[#Repeat="true"]]
</Binding.XPath>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<StackPanel
Orientation="Horizontal"
VerticalAlignment="Center">
<CheckBox
Margin="5,0,0,0" />
<TextBlock
Margin="5,0,0,0"
Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>

Why my autocompletebox does not display any data?

I have a silverlight autocompletebox, am adding the ItemsSource in the codebehind.cs , also set the valuemember path as well, it does not display any data.
here is the code,
<sdk:AutoCompleteBox Margin="105,2,40,0" ItemsSource="{Binding}" ValueMemberPath="Code" FilterMode="Contains" IsTextCompletionEnabled="True" x:Name="txtcode" Height="23" VerticalAlignment="Top" TabIndex="1" TabNavigation="Local" >
<sdk:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock />
</DataTemplate>
</sdk:AutoCompleteBox.ItemTemplate>
</sdk:AutoCompleteBox>
txtcode.ItemsSource = collection;
Possibly you have missed this one inside the DataTemplate,
<TextBlock Text="{Binding Code}" />
You Should bind that textbox first
Sample Code:
<sdk:AutoCompleteBox Margin="105,2,40,0" ItemsSource="{Binding}" ValueMemberPath="Code" FilterMode="Contains" IsTextCompletionEnabled="True" x:Name="txtcode" Height="23" VerticalAlignment="Top" TabIndex="1" TabNavigation="Local" >
<sdk:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Item}" />
</DataTemplate>
</sdk:AutoCompleteBox.ItemTemplate>
</sdk:AutoCompleteBox>
txtcode.ItemsSource = collection;

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 to viewmodel from inside a datatemplate

I have multiple videos displayed they are bound with a videocollection in Mainviewmodel. Everything works fine untill I try to bind the enter command to Mainviewmodel. I Don't know the syntax for this. As it stands the binding is set to Video and not Mainviewmodel.
Errormessage:
'StartVideoCommand' property not found on 'object' ''Video'
Xaml:
<Window.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MainViewModel}">
<ListBox ItemsSource="{Binding Videos}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.InputBindings>
!!! <KeyBinding Key="Enter" Command="{Binding StartVideo}" /> !Bound to Video not to Mainviewmodel grrr
</Grid.InputBindings>
... layout stuff
<TextBlock Text="{Binding Title}" Grid.Column="0" Grid.Row="0" Foreground="White"/>
<TextBlock Text="{Binding Date}" Grid.Column="0" Grid.Row="1" Foreground="White" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Length}" Grid.Column="1" Grid.Row="1" Foreground="White" HorizontalAlignment="Right"/>
... closing tags
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.StartVideo}"
Another approach would be to use ElementName binding instead of RelativeSource.
Example:
<Window x:Name="root" ... >
...
Command="{Binding ElementName=root, Path=DataContext.StartVideo}"
...
A possible advantage over RelativeSource is that this is explicit; if someone changes the XAML hierarchy then relative references could break unintentionally. (Not likely in this specific example of binding to a Window however).
Also if your "root" element already is named then so much the better it is easy to take advantage of.
It is also somewhat more readable.

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