XAML - Binding to DataContext and using converter? - silverlight

To bind to the current DataContext in XAML you can use:
<TextBlock Text="{Binding}" />
How do you do this using a converter in the mix?
The following works when you have a property on the path:
<TextBlock Text="{Binding MyProperty,Converter={StaticResource converter}}" />
But I dont want to do that; I just want to Bind to the datacontext and not the datacontext.MyProperty if you get what I mean.

Simply omit the path:
<TextBlock Text="{Binding Converter={StaticResource converter}}" />
Ah wait - I notice your question is tagged with Silverlight. Does this not work in Silverlight? If not, you may need to use the expanded syntax:
<TextBlock>
<TextBlock.Text>
<Binding Converter="{StaticResource converter}" />
</TextBlock.Text>
</TextBlock>

Dot sign also provide DataContext Binding for SL developers
<TextBlock Text="{Binding Path=.,Converter={StaticResource converter}}" />

Related

How can I make a control in Wpf depend on a property in viewmodel?

I have this codes, but I don't want just make control visible or invisible , I don't want to create it, because it works in background and does some work that takes time and I don't want it. how can I do that? Can I use if condition in xaml in wpf? I've heard about triggers but I don't know how to do that in my example?
<ContentControl Content="{Binding ViewService[NoteSaveView]}" Visibility="{Binding ViewModeVisibility[Insert]}"/>
<telerik:RadTabControl TabOrientation="Horizontal" TabStripPlacement="Right" Padding="5" BorderThickness="0" Margin="0 -5 -5 -5" Visibility="{Binding ViewMode,Converter={StaticResource UpdateViewToVisibility}}">
<telerik:RadTabItem Header="صفحه اصلی" Content="{Binding ViewService[NoteSaveView]}"/>
<telerik:RadTabItem Header="تاریخچه" Content="{Binding ViewService[LogView]}"/>
</telerik:RadTabControl>
Edit:
finally I solved my problem with this code:
<ContentControl>
<ContentControl.Content>
<MultiBinding Converter="{StaticResource ViewModeVisibilityToContent}">
<Binding Path="ViewModeVisibility[Insert]"/>
<Binding Path="ViewService[NoteSaveView]"/>
</MultiBinding>
</ContentControl.Content>
</ContentControl>
You could use a DataType property of a DataTemplate and then assign a data object to the property to activate a DataTemplate or null to deactivate it.
In the code below you just need to set PropertyInViewModel of type object in viewmodel to some int to activate the DataTemplate, or to null to deactivate it.
<StackPanel xmlns:System="clr-namespace:System;assembly=mscorlib">
<StackPanel.Resources>
<DataTemplate DataType="{x:Type System:Int32}">
<TextBox Text="{Binding Mode=OneWay}" Foreground="Beige" Background="Brown" Height="20" Width="50"/>
</DataTemplate>
</StackPanel.Resources>
<ContentControl Content="{Binding PropertyInViewModel}"/>
</StackPanel>

binding Textblock from two textboxex value in Xaml

I have two textbox txtFName and txtLName . Now I want to display txtFName - txtLName in textblock using binding.
For bind only txtFName I write below code:
<TextBlock x:Name="textblock" Text="{Binding ElementName=txtFName , Path=Text}" Margin="-3,-8,0,0"/>
But I want to display txtFName - txtLName in textblock using binding.
I do not want to write any code in code behind.
Thanks,
You can do that using MultiBinding:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding ElementName="txtFName" Path="Text"/>
<Binding ElementName="txtLName" Path="Text"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The content of a TextBlock is composed of a collection of inline objects, e.g. Runs. Therefore, you can bind txtFName and txtLName to two different runs, e.g. like this:
<TextBlock x:Name="textblock">
<Run Text="{Binding ElementName=txtFName, Path=Text}"/>
<Run Text=" - "/>
<Run Text="{Binding ElementName=txtLName, Path=Text}"/>
</TextBlock>

WPF StringFormat databinding text not appearing

I am trying to get the header text of a TreeviewItem to be set in the binding to an XML source. Everything is working fine except the only thing that appears in the header is the text I'm binding to and nothing else in the string format. Example:
<HierarchicalDataTemplate x:Key="LogDataTemp" ItemsSource="{Binding Path=log}">
<TreeViewItem>
<TreeViewItem.Header>
<Binding Path="Attribute[level].Value" StringFormat="TEST \{0\}" />
</TreeViewItem.Header>
</TreeViewItem>
</HierarchicalDataTemplate>
In this case the level value appears but nothing else. I have tried dozens of different ways of approaching this and it seems that nothing works.
Don't escape the braces in the StringFormat. You want to apply the formatting to the 0th-element in your binding.
For instance, with a simple property called "Level":
<TextBlock x:Name="txtUnformatted" Grid.Row="0" Foreground="White" >
<TextBlock.Text>
<Binding Path="Level" />
</TextBlock.Text>
</TextBlock>
<TextBlock x:Name="txtFormatted" Grid.Row="1" Foreground="White">
<TextBlock.Text>
<Binding Path="Level" StringFormat="Test {0:000000}" />
</TextBlock.Text>
</TextBlock>
And the result is something like:
Update
Also, the default implementation of the Header, when you don't add any controls, is a simple ContentPresenter, which doesn't apply formatting. To work around this, simply put a TextBlock into the header, and bind the text you want formatted to that.
<HierarchicalDataTemplate x:Key="LogDataTemp" ItemsSource="{Binding Path=log}">
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock>
<TextBlock.Text>
<Binding Path="Attribute[level].Value"
StringFormat="TEST {0}" />
</TextBlock.Text>
</TextBlock>
</TreeViewItem.Header>
</TreeViewItem>
</HierarchicalDataTemplate>
It is perfectly acceptable (and commonly done) to put controls into a header control (for instance, a grid containing an image and a label). The beauty of WPF.

WPF Commands Buttons ListBoxItems

I Have a listbox defined like below :
<ListBox x:Name="lstMedias" ItemsSource="{Binding Medias}" Width="Auto" Height="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}" Tag="{Binding Name}" Click="Button_Click" Command="{Binding Path=LoadSimpleMoviePopupCommand}">
<Button.Resources>
<Converters:LoadMovieMultiConverter x:Key="LoadMovieMultiConverter" />
</Button.Resources>
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource LoadMovieMultiConverter}">
<MultiBinding.Bindings>
<Binding ElementName="DragDropCanvas" />
<Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
</MultiBinding.Bindings>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When Trying to call command LoadSimpleMoviePopupCommand, command is not called, but when calling click event, event is raised.
Do You have an idea why ? It is a normal behavior ? Do we have to trick like ListBoxItem doubleclick ?
Probably because the binding failed. Check the output window of VS and see if there are any binding errors. I suspect the LoadSimpleMoviePopupCommand property is not on your data item class (ie. your Media class).
had the same problem, try this
<Button Command="{Binding DataContext.YourCommand,RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
it's quite normal, he can't find your command binding inside the listbox because you set something like
<DataTemplate DataType ...
in that listbox so he'll be looking for that binding inside the datatype and not the viewmodel (I assume :)

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