Hi
In my Grid I have dynamicly added columns that in some cases have child columns. In the parent column's Label there is a button that sets the visibility of it's child columns.
My problem is that I want to change the content of the button when it's clicked. When the children are collapesed the button's content should be ">>" and when they are visible it should be "<<".
I tried to make a new button class and add a dependency property "IsExtended" and set the content in a style. But this only works on a button thats not in the grid.
Can someone tell me what I'm doing wrong?
/Sara
<Style x:Key="HeaderShowChildren" TargetType="{x:Type igDP:LabelPresenter}" BasedOn="{StaticResource DefaultLabelStyle}">
<Setter Property="ContentTemplate" > <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Top" Height="80" Width="60" TextWrapping="Wrap" Margin="5,0,0,0" TextTrimming="CharacterEllipsis" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource TextBlockToolTip}"/> <Canvas VerticalAlignment="Top"> <Recipients:ExtendedButton Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" Click="ExtendedButton_Click" Margin="2" FontSize="8" Width="20" Height="15" Style="{StaticResource ExtendedButtonStyle}"/> </Canvas> </StackPanel> </DataTemplate> </Setter.Value> </Setter>
<Setter Property="ContentTemplate" >
<Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Top" Height="80" Width="60" TextWrapping="Wrap" Margin="5,0,0,0" TextTrimming="CharacterEllipsis" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource TextBlockToolTip}"/> <Canvas VerticalAlignment="Top"> <Recipients:ExtendedButton Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" Click="ExtendedButton_Click" Margin="2" FontSize="8" Width="20" Height="15" Style="{StaticResource ExtendedButtonStyle}"/> </Canvas> </StackPanel> </DataTemplate> </Setter.Value>
<Setter.Value>
<DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Top" Height="80" Width="60" TextWrapping="Wrap" Margin="5,0,0,0" TextTrimming="CharacterEllipsis" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource TextBlockToolTip}"/> <Canvas VerticalAlignment="Top"> <Recipients:ExtendedButton Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" Click="ExtendedButton_Click" Margin="2" FontSize="8" Width="20" Height="15" Style="{StaticResource ExtendedButtonStyle}"/> </Canvas> </StackPanel> </DataTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Top" Height="80" Width="60" TextWrapping="Wrap" Margin="5,0,0,0" TextTrimming="CharacterEllipsis" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource TextBlockToolTip}"/> <Canvas VerticalAlignment="Top"> <Recipients:ExtendedButton Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" Click="ExtendedButton_Click" Margin="2" FontSize="8" Width="20" Height="15" Style="{StaticResource ExtendedButtonStyle}"/> </Canvas> </StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Top" Height="80" Width="60" TextWrapping="Wrap" Margin="5,0,0,0" TextTrimming="CharacterEllipsis" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource TextBlockToolTip}"/> <Canvas VerticalAlignment="Top"> <Recipients:ExtendedButton Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" Click="ExtendedButton_Click" Margin="2" FontSize="8" Width="20" Height="15" Style="{StaticResource ExtendedButtonStyle}"/> </Canvas>
<TextBlock VerticalAlignment="Top" Height="80" Width="60" TextWrapping="Wrap" Margin="5,0,0,0" TextTrimming="CharacterEllipsis" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource TextBlockToolTip}"/>
<Canvas VerticalAlignment="Top">
<Recipients:ExtendedButton Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" Click="ExtendedButton_Click" Margin="2" FontSize="8" Width="20" Height="15" Style="{StaticResource ExtendedButtonStyle}"/>
</Canvas>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ExtendedButtonStyle" TargetType="{x:Type ExtendedButton }">
<Style.Triggers> <Trigger Property="IsExtended" Value="true"> <Setter Property="Content" Value="<<"/> </Trigger> <Trigger Property="IsExtended" Value="false"> <Setter Property="Content" Value=">>"/> </Trigger> </Style.Triggers>
<Style.Triggers>
<Trigger Property="IsExtended" Value="true"> <Setter Property="Content" Value="<<"/> </Trigger> <Trigger Property="IsExtended" Value="false"> <Setter Property="Content" Value=">>"/> </Trigger>
<Trigger Property="IsExtended" Value="true">
<Setter Property="Content" Value="<<"/>
</Trigger>
<Trigger Property="IsExtended" Value="false">
<Setter Property="Content" Value=">>"/>
</Style.Triggers>
{
ChangeButtonContent((ExtendedButton)sender);
}
button.IsExtended = !button.IsExtended;
public class ExtendedButton: Button
public static readonly DependencyProperty IsExtendedProperty = DependencyProperty.Register("IsExtended", typeof (bool), typeof (ExtendedButton));public bool IsExtended { get { return (bool) GetValue(IsExtendedProperty); } set { SetValue(IsExtendedProperty, value); }
typeof (ExtendedButton));
get { return (bool) GetValue(IsExtendedProperty); } set { SetValue(IsExtendedProperty, value); }
set
SetValue(IsExtendedProperty, value);
Hi Alex.
Yes I noticed that it works when i comment ResetVisibility(). But why does changing the visibilty reset it?
Sara
Hello Sara,
The problem is here :
private void ExtendedButton_Click(object sender, RoutedEventArgs e) { ChangeButtonContent((ExtendedButton)sender); //MessageBox.Show((sender as ExtendedButton).IsExtended.ToString()); ResetVisibility(); }
If you uncomment the bolded line, you will see that even the buttons in the grid are switching from ">>" to "<<" right at the point of the MessageBox showing, but the function ResetVisibility puts them back to ">>". Maybe aware of that you will be able to fix the problem or get around it.
Hope this helps,
Alex.
Hello
Here is a sample where we change the visablility on the last column and change the text on the button. It works on the button outside the datagrid but not on the ones inside.
I was trying to reproduce this scenario but I encountered some difficulties doing it -- mainly with the style and the registered DependencyProperty (IsExtended). If you give me some more code or a piece of the project I would be able to be of greater assistance.