Unable to disable textblock text in the itemtemplate - wpf

<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<StackPanel Orientation="Horizontal" Width="150" >
<TextBlock MaxWidth="125" Name="name" Text="{Binding name}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" Margin="0,4,4,4" />
</StackPanel>
<Button Command="{buttontext}" Visibility="{Binding IsAvailable, Converter={uil:BoolToVisibilityConverter}}">
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="{Binding Isenable}"/>
</Style>
</ListBox.ItemContainerStyle>
I am unable to disable the text(Grey out) but as for the button is working fine.
Do anyone know how to solve this issue?
Hi Chris, i have referred to the link. i tried using
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding name}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
but it is still not working. have any idea on this?

TextBlock doesn't support IsEnabled. The easiest fix would be to change your TextBlock to a Label

Related

WPF Select ComboBox text style based on variable

I have a ComboBox and I'd like to be able to change the style of the text inside the ComboBoxItem based on an attribute of the ComboBoxItem.
<ComboBox ItemsSource="{Binding Countries}">
<ComboBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" Focusable="False">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource MyStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ComboBox.ItemTemplate>
I've been trying to set the TextBlock Style from MyStyle to a different one if the Country's attribute IsWestern is true, but I am not managing. I figure I need a Trigger somewhere, but I can't figure it out and added a variety, but none worked so far.
use DataTrigger to change ContentTemplate:
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource MyStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWestern}" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource WesternStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
alternatively include <DataTrigger Binding="{Binding Path=IsWestern}" Value="True"> in TextBlock Style (MyStyle) and change required TextBlock properties there:
<Style x:Key="MyStyle" TargetType="TextBlock">
<Setter Property="LineStackingStrategy" Value="BlockLineHeight"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWestern}" Value="True">
<Setter Property="Padding" Value="20,0,0,0"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>

Two bindings; One Works, Other Doesn't. Why?

Why does one label update but not the other? Both bound to the same property. I assume there's a problem with the binding being in a dataTemplate? Using Resharper I'm told that my lblOverallInt cannot resolve the symbol. How could I fix this?
<Label Name="lbl1" Content="{Binding Path=lblOverallInt, UpdateSourceTrigger=PropertyChanged}"/>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Name="lbl2" Content="{Binding Path=lblOverallInt, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
UPDATE
Related follow on question; I have the following style binding also which works when applied to the label but not to the Expander. Is there a similar process for wiring this up as mm8 solution to the top part of this question?
Added separate solution for this part
<Expander.Style>
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=lblOverallInt}" Value="0">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=lblOverallInt, Converter={StaticResource isZeroConverter}}" Value="False">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
The DataContext of the HeaderTemplate is the header itself. Try this:
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Name="lbl2" Content="{Binding Path=DataContext.lblOverallInt, RelativeSource={RelativeSource AncestorType=Expander}}"/>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
Or this:
<Expander Header="{Binding Path=lblOverallInt}">
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Name="lbl2" Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
For part 2 of my question I had the style binding part still attached to the Expander when it actually needed to be included in the DataTemplate;
<Expander.HeaderTemplate>
<DataTemplate>
<Border Height="24">
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Resources>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="0">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding Converter={StaticResource isZeroConverter}}" Value="False">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
</StackPanel.Style>
<Label>Errors/Warnings:</Label>
<Label Name="lbl2" Content="{Binding}"/>
</StackPanel>
</Border>
</DataTemplate>
</Expander.HeaderTemplate>

`TextBlock`'s `TextWrapping` property not getting set to `Wrap` on conditions

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.

WPF - How to change Image in DataGrid column upon selection of row (in XAML)?

Our project has a DataGrid that displays an image along with text in one of the columns. When a row is selected, since it's highlighted, the image is not visible correctly. Hence we want to change the image only for the selected row.
I know how to change the property such as background of the dataGridCell using <Style.Triggers>; but cannot figure out how to change Image that is embedded inside the <DataGridTemplateColumn.CellTemplate>. Can you please help me with this?
<DataGrid Name="dgCPAGrid" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding CPAListDisplay, Mode=OneWay}" Margin="0,5,0,5" AutoGenerateColumns="False"
IsReadOnly="True" SelectionMode="Single"
SelectedItem="{Binding SelectedCPA, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False" AlternatingRowBackground="White"
HorizontalGridLinesBrush="Gray" VerticalGridLinesBrush="Gray" >
<DataGrid.Resources>
<Style x:Key="DGCellMGA" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
<Setter Property="Background" Value="#f8f8d2" />
<Setter Property="TextBlock.TextAlignment" Value="Right"/>
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="{b:CaptionBinding gridHeaderCPAName}" Width="Auto" MinWidth="125">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CPAName}" />
<Image Source="/Images/TimelineIconGreenTransparent.gif" Margin="5,0,0,0" Height="15" Width="15" Visibility="{Binding StaticInd, Converter={StaticResource BoolVisConv}, ConverterParameter=inverse, Mode=OneWay}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{b:CaptionBinding gridHeaderCPAMGA}" Binding="{Binding MgaY1P}" MinWidth="50" Width="*" CellStyle="{StaticResource DGCellMGA}" />
</DataGrid.Columns>
</DataGrid>
Inside your cell's Template, add another Trigger (this time, a DataTrigger) that listens to the IsSelected property of the containing DataGridRow. Inside the DataTrigger, use the TargetName property of the Setter to tell it to modify the Source property of the Image (you have to give it an x:Name first):
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CPAName}" />
<Image x:Name="image" Source="/Images/TimelineIconGreenTransparent.gif" Margin="5,0,0,0" Height="15" Width="15" Visibility="{Binding StaticInd, Converter={StaticResource BoolVisConv}, ConverterParameter=inverse, Mode=OneWay}"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}"
Value="True">
<Setter TargetName="image"
Property="Source"
Value="/Images/Whateveryourotherimageisnamed.gif" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

WPFdatagrid - how to use(give) different colors while grouping?

I am using WPF datagrid by codeplex.
I have a wpf grid with grouping features. I want the grouped region in different colors.
The screenshot is as follows:
Can different colors be assigned during grouping? If yes how do I achieve this in WPF datagrid?
Hope this helps...
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" "/>
<TextBlock Text="Items"/>
<TextBlock Text=")"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter>
<ItemsPresenter.Resources>
<Style TargetType="{x:Type toolkit:DataGridRow}">
<Style.Triggers>
<DataTrigger
Binding="{Binding RelativeSource=
{RelativeSource AncestorType={x:Type
GroupItem}}, Path=DataContext.Name}"
Value="1">
<Setter Property="Background"
Value="LightGreen"/>
</DataTrigger>
<DataTrigger
Binding="{Binding RelativeSource=
{RelativeSource AncestorType={x:Type
GroupItem}}, Path=DataContext.Name}"
Value="2">
<Setter Property="Background"
Value="LightPink"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsPresenter.Resources>
</ItemsPresenter>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
The data triggers above check which value we have created the groups upon and accordingly assigns data grid row background colors.
So first group represents all values under text "1" (LightGreen) and next group is grouped under value 2 (LightPink).

Resources