I have a WPF treeview that I would like the color of a node to be based on a particular getter. I can't figure how to databind for that case. I would like it to look like this except that odd numbers would be a child node of the even numbers
If you already use HierarchicalDataTemplate, you can simply add a trigger:
<TreeView ItemsSource="{Binding}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Highlight}" Value="True">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Related
Good afternoon,
I´m binding a list of doubles (Latlng) to an ItemsControl and in a TextBlock I want to make a custom text when the list binded has a count of 0. With the code I have the TextBlock is empty when the ItemsSource of the ItemsControl is that list.
What am I doing wrong?
Btw the list Latlng is a property of a class.
<ItemsControl Name="icLatLng" ItemsSource="{Binding Latlng}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock FontFamily="Arial" FontSize="14">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.Text" Value="{Binding}"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=Items.Count}" Value="0">
<Setter Property="TextBlock.Text" Value="—"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Thanks in advance.
Welcome to SO.
ItemsControl.ItemTemplate is the template that gets applied to each element in the Items list the control is bound to. If there are no items to begin with, then it won't be created, so anything you do in it won't be seen.
I suspect what you're really trying to do is replace the look of the entire control when the list is empty. If so, you can do that by applying a new template to the ItemsControl itself using a DataTrigger on the HasItems property:
<ItemsControl Name="icLatLng" ItemsSource="{Binding Latlng}">
<ItemsControl.Style>
<Style TargetType="{x:Type ItemsControl}" BasedOn="{StaticResource {x:Type ItemsControl}}">
<Style.Triggers>
<DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<TextBlock Text="-" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
The TextWrapping property was setting to Wrap in this code
<ListView Name="answerListView" ItemsSource="{Binding Path=answers}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Expander Cursor="Hand">
<Expander.Header>
<TextBlock Text="{Binding Path=Body_Markdown}" TextWrapping="Wrap"/>
</Expander.Header>
</Expander>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
But, now I have added conditional formatting to the TextBlock, i.e., if answer is accepted then show it in green colour. So the code I have used is this:
<ListView Name="answerListView" ItemsSource="{Binding Path=answers}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Expander Cursor="Hand">
<Expander.Header>
<TextBlock Text="{Binding Path=Body_Markdown}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=is_accepted}" Value="true">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="TextWrapping" Value="Wrap"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Expander.Header>
</Expander>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Here, the second line of setting, i.e., Foreground property is not working. Even if I have the same line after <Style TargetType> or TextWrapping="Wrap" in TextBlock, then also it is not working.
ScrollViewer.HorizontalScrollBarVisibility="Disabled" must been added to ListView. Then this problem will be solved.
Specify the type too:
<Setter Property="TextBlock.Foreground" Value="Green"/>
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
I guess you are binding a private field, instead of the public property:
{Binding Path=is_accepted} should be replaced by your property {Binding Path=Is_accepted}
This answer assumes that you are using the usual naming which makes fields start by lower case and Properties by Upper case.
I have a ListView which I want to present different kinds of user controls, depending on which view model is set for the list view item.
In xaml:
<ListView ItemsSource="{Binding Items}">
<ListView.Resources>
<DataTemplate DataType="{x:Type viewModels:LabelledTextViewModel}">
<controls:LabelledTextBox/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:FolderChooserViewModel}">
<standardControls:FolderChooser/>
</DataTemplate>
</ListView.Resources>
</ListView>
Now this works fine, but my LabelledTextViewModel can be editable or not editable. How do I say in XAML to check the property "IsEditable" on my viewmodel, and depending on its value show LabelledTextBlockControl or LabelledTextBoxControl?
You can use a DataTrigger in your DataTemplate:
<ListView ItemsSource="{Binding Items}">
<ListView.Resources>
<DataTemplate DataType="{x:Type viewModels:LabelledTextViewModel}">
<Grid>
<controls:LabelledTextBlockControl x:Name="textBlock"/>
<controls:LabelledTextBoxControl x:Name="textBox" Visibility="Collapsed"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter TargetName="textBlock" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="textBox" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:FolderChooserViewModel}">
<standardControls:FolderChooser/>
</DataTemplate>
</ListView.Resources>
</ListView>
For your requirements, you either need to use the DataTemplateSelector Class to make that selection for you, or you could try to name your DataTemplates and set them using a DataTrigger:
<ListView>
<ListView.Resources>
<DataTemplate x:Key="DefaultDataTemplate" DataType="{x:Type viewModels:LabelledTextViewModel}">
<controls:LabelledTextBox/>
</DataTemplate>
<DataTemplate x:Key="AnotherDataTemplate" DataType="{x:Type viewModels:FolderChooserViewModel}">
<standardControls:FolderChooser/>
</DataTemplate>
</ListView.Resources>
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="ItemTemplate" Value="{StaticResource DefaultDataTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter Property="ItemTemplate" Value="{StaticResource AnotherDataTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
This example assumes that you are setting the DataTemplates to the ItemTemplate property... if not, you'll need to change that property to the relevant one.
I have the following DataTemplate in resources that I would like to reuse throughout a GridView.
<Window.Resources>
<DataTemplate x:Key="NumericalDataTemplate" DataType="GridViewColumn.CellTemplate">
<StackPanel Orientation="Horizontal" Height="32">
<TextBlock Text="{Binding MyLength}" VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}" >
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding PropertyEditable}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources>
Which is implemented as follows.
<GridViewColumn Header="MyLength" Width="80"
CellTemplate="{StaticResource NumericalDataTemplate}" />
I would like change the Binding of the TextBlock (Currently Text={Binding MyLength} ) so that it can use a custom binding for each GridViewColumn Cell Template (eg MyHeight, MyWeight etc).
The way I envisaged doing this is changing the Binding of the TextBlock to simply use {Binding} and having the GridViewColumn set the Binding. However, I'm not sure where or how to do this, as setting the DisplayMemberValue to {Binding MyLength} (for example) simply overrides the template.
I would preferably like to do this entirely in XAML.
It seems that CellTemplate will always be ignored when we have DisplayMemberBinding property set. Possible workaround for this limitation is, by creating markup-extension as pointed by #H.B in his answer to similar question here. Creating markup-extension involves C#/VB codes, but using it only needs XAML codes.
You can reuse the same markup-extension C# code provided by #H.B. Then to use it in your XAML, declare namespace prefix :
<Window ......
xmlns:local="clr-namespace:WpfProject">
Modify DataTemplate key and binding of the TextBlock inside :
<DataTemplate x:Key="TemplateBuilder_BaseTemplate" DataType="GridViewColumn.CellTemplate">
<StackPanel Orientation="Horizontal" Height="32">
<TextBlock Text="{local:TemplateBuilderTag}" VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}" >
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding PropertyEditable}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</DataTemplate>
Now you can use the same DataTemplate for different column binidngs :
<GridView.Columns>
<GridViewColumn Header="MyLength" Width="80"
CellTemplate="{local:TemplateBuilder MyLength}" />
<GridViewColumn Header="MyHeight" Width="80"
CellTemplate="{local:TemplateBuilder MyHeight}" />
</GridView.Columns>
I have a comboBox that is bound to a list of strings from my viewModel. What I am trying to do is have the foreground of a comboBox item be set to a different color if a property in my viewModel is true:
<ComboBox x:Name="myComboBox" ItemsSource="{Binding Names}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ...}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="True">
<Setter Property="Foreground" Value="Navy"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I am not sure what to bind the Text of the TextBlock to. All I want is to display the list of strings. I always end up with a dropdown that has the items but they are not visible. I tried removing the style trigger thinking that maybe I was screwing up there, but that didn't help.
Am I taking the right approach? Will the ComboBox.ItemTemplate correctly look at my viewModel (which is the data context) when searching for IsActive or is that binding incorrect as well?
The DataContext for each ComboBoxItem is a string so
For the TextBlock, bind to the DataContext like Text="{Binding}
For the DataTrigger to be able to find IsActive, use RelativeSource in the binding
<ComboBox x:Name="myComboBox" ItemsSource="{Binding Names}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}},
Path=DataContext.IsActive}"
Value="True">
<Setter Property="Foreground" Value="Navy"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>