I have a data bound tab control:
<TabControl ItemsSource="{Binding Products}" Name="ProductsTabControl">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
This control is showing one tab per product, however I would like to make the tabs of discontinued products semi-transparent (i.e. set their opacity to 0.2). How can I change the opacity property of the tabitem when the item is being auto generated. I know I could use a style to change them all, but I only want to change those which are discontinued.
In ItemsContainerStyle for TabControl, create a DataTrigger where you bind to your property (e.g IsDiscontinued) and set the Opacity from there
<TabControl ItemsSource="{Binding Products}" Name="ProductsTabControl">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsDiscontinued}" Value="True">
<Setter Property="Opacity" Value="0.2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.ItemContainerStyle>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
Update
If you want to make the Content of the discontinued tabs semi-transparent you can do the same thing, but in the DataTemplate
<TabControl ItemsSource="{Binding Products}" Name="ProductsTabControl">
<TabControl.Resources>
<DataTemplate DataType="{x:Type local:Product}">
<Border Name="bg" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding Name}"/>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsDiscontinued}" Value="True">
<Setter TargetName="bg" Property="Opacity" Value="0.2"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</TabControl.Resources>
<!--...-->
</TabControl>
Related
I've got a list box with SelectionMode set to Single and item template looking like this:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="100" Margin="10" Cursor="Hand" >
<Image Source="/Assets/Images/folder_80closed.png" HorizontalAlignment="Center" />
<TextBox Text="{Binding Name}" BorderThickness="0" TextAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Is there any way to change the value of image Source property in XAML based on if the item is selected or not? Something like pic bellow, where item4 is selected.
You may use an Image Style with a DataTrigger on the IsSelected property of the current ListBoxItem:
<Image HorizontalAlignment="Center">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source"
Value="/Assets/Images/folder_80closed.png"/>
<Style.Triggers>
<DataTrigger
Binding="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
Value="True">
<Setter Property="Source"
Value="/Assets/Images/some_other_image.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Each tab contains ListView & I need to change ItemsSource property of this ListView each time when user selects different tab. How can I do this in XAML? I have tried
<TabItem Header="F2" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t2">
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=Self}}" Value="True">
<Setter TargetName="listView1" Property="ItemsSource" Value="{Binding t2C}"></Setter>
</DataTrigger>
</TabItem>
but element named listView1 is inaccessible.
<Window.Resources>
<DataTemplate x:Key="TabItemTemplate">
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<ListView x:Name="listView1" ItemsSource="{Binding NewsPropCollection}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<!-- Some code here -->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid/>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl x:Name="tabControl" Grid.Row="1">
<TabItem Header="F1" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t1" IsSelected="True"/>
<TabItem Header="F2" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t2"></TabItem>
<TabItem Header="F3" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t3"></TabItem>
<TabItem Header="F4" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t4"></TabItem>
<TabItem Header="F5" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t5"></TabItem>
</TabControl>
</Grid>
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 an textblock, expander and a textbox...
these are inside the header of a listview column.
TextBlock is for listview column name, on click of expander...textbox will be displayed...and user can seartch the listview based on that column. The textbox is collapsed by default.
My requirement is, when user click on the expander, textbox should be displayed to the user...and focus should be on the textbox.
With the below XAML, I am able to display the textbox on click of expander and set the focus(cursor) on my textbox. But that cursor is no blinking. I mean I have to again click on textbox to type something
Please help me to find out what the issue is...Any help would be appreciated.
<StackPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="ID"/>
<Expander x:Name="IdExp" DockPanel.Dock="Right" IsExpanded="False" ExpandDirection="Down" >
</Expander>
</DockPanel>
<TextBox x:Name="PropertyCCCIDSearch"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},
Path=DataContext.SearchCCGId.Value,UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding ElementName=IdExp, Path=IsExpanded, Converter={x:Static local:Converters.BoolToVisibility}}" >
<TextBox.Style>
<Style>
<Style.Triggers> <DataTrigger Binding="{Binding ElementName=IdExp, Path=IsExpanded}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=PropertyCCCIDSearch}"/> </DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
Your code is working correctly this is what i tried
<Window.Resources>
<BooleanToVisibilityConverter x:Uid="BooleanToVisibilityConverter_1" x:Key="b2v" />
</Window.Resources>
<Grid>
<StackPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="ID"/>
<Expander x:Name="IdExp" DockPanel.Dock="Right" IsExpanded="False" ExpandDirection="Down" >
</Expander>
</DockPanel>
<TextBox x:Name="PropertyCCCIDSearch" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},
Path=DataContext.SearchCCGId.Value,UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding ElementName=IdExp, Path=IsExpanded,
Converter={StaticResource b2v}}" >
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=IdExp, Path=IsExpanded}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=PropertyCCCIDSearch}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
</Grid>
</Window>
I have a listbox where the itemtemplate is using a style. The styles specifies a border with a datatrigger setting the visibility of the border to collapsed depending on a property. This works fine except I can still see a very narrow line for each item, in the list, that is collapsed. I was hoping someone could help with how to set the visibility so that there are no visible traces as this is quite apparent when consecutive items have been collapsed.
The datatemplate specifies an outer border with a dockpanel inside of this - there are then stackpanels docked to this.
Any help is appreciated.
Well this is a simplified template:
<DataTemplate x:Key="myTemplate">
<Border BorderThickness="0">
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsActive}" Value="False">
<Setter Property="Border.Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<StackPanel DockPanel.Dock="Right" HorizontalAlignment="Right" >
<TextBlock Text="{Binding Path=SeqNo, Converter={StaticResource SeqToTextConv}}"/>
<Label Content="..." />
</StackPanel>
</DockPanel>
</Border>
</DataTemplate>
You are succesfully hiding your item, however, the ListBox wraps each of your items within a ListBoxItem, this adds concepts such as selection to your item. I suspect you are still seeing the ListBoxItem in the case where your items are hidden. You can use the ItemContainerStyle to hide ListBoxItems ...
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
This can also be achieved by populating ListBox.Items only with ListBoxItem instead of other controls:
ListBox.Items.Add(new ListBoxItem {
Content = new CheckBox {Content = "item 1"}
})
or
<ListBox>
<ListBox.Items>
<ListBoxItem>
<CheckBox Content="item 1"/>
</ListBoxItem>
</ListBox.Items>
</ListBox>
Then in the code behind or in the trigger, you can hide the items directly:
ListBox.Items[0].Visibility = Visibility.Collapse
This will hide the item as well as the 4px border. This method gives you control of visibility for each individual item.
I went with ColinE's proposed solution. Here is a full snipped for everybody's convenience. Thanks ColienE.
<ListBox ItemsSource="{Binding Properties}" Height="110">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}" Visibility="{Binding Visible}" />
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Visible}" Value="Collapsed">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>