none
GridviewcolumHeader RadioButtons RRS feed

  • Question

  • Hi All,

    I have a listview with gridviewclumheader template which exposes RadioButton for each Header as such:

    <Style x:Key="CheckedHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">

    <Setter Property="GridViewColumnHeader.Template">

    <Setter.Value>

    <ControlTemplate>

    <StackPanel Orientation="Horizontal">

    <RadioButton x:Name="PART_RBVis" Checked="RBVis_Checked"/>

    </StackPanel>

    </ControlTemplate>

    </Setter.Value>

    </Setter>

    </Style>

     

    I want that each time I check a radio button in one header, all other radiobuttons(located in the other Headers) will be unchecked.

     

    what can I do?

     

    Thanks

     

     

    Thursday, March 13, 2008 2:28 PM

Answers

  • Okay, I guess you are still using Visual Studio 2005, here is the modified code which should work under .NET Framework 3.0:

    public static class VisualHelper
    {
        public static T GetVisualChild<T>(Visual referenceVisual) where T : Visual
        {
            Visual child = null;
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
            {
                child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
                if (child != null && child is T)
                {
                    break;
                }
                else if (child != null)
                {
                    child = GetVisualChild<T>(child);
                    if (child != null && child is T)
                    {
                        break;
                    }
                }
            }
            return child as T;
        }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            btn.Click += delegate
            {
                GridViewHeaderRowPresenter headerRow = listView.GetVisualChild<GridViewHeaderRowPresenter>();
                if (headerRow != null)
                {
                    IEnumerable children = LogicalTreeHelper.GetChildren(headerRow);
                }
            };
        }

        private void GridViewColumnHeader_Checked(Object sender, RoutedEventArgs e)
        {
            ContentPresenter presenter = ((RadioButton)sender).TemplatedParent as ContentPresenter;
            GridViewColumnHeader header = null;
            if (presenter != null)
            {
                header = presenter.TemplatedParent as GridViewColumnHeader;
            }

            GridViewHeaderRowPresenter headerRow = VisualHelper.GetVisualChild<GridViewHeaderRowPresenter>(listView);
            if (headerRow != null && header != null)
            {
                foreach (DependencyObject dependencyObject in LogicalTreeHelper.GetChildren(headerRow))
                {
                    GridViewColumnHeader gvch = dependencyObject as GridViewColumnHeader;
                    if (gvch != null && gvch != header)
                    {
                        gvch.Tag = false;
                    }
                }
            }
        }
    }
    Tuesday, March 18, 2008 8:20 AM

All replies

  • Here is an example:

    Code Snippet

    <Window x:Class="AnswerHarness2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1">
      <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
          <StackPanel.Resources>
            <DataTemplate x:Key="myHeaderTemplate">
              <RadioButton
                IsChecked="{Binding Path=Tag, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumnHeader}}, Mode=TwoWay}"
                Checked="GridViewColumnHeader_Checked"
                Content="{Binding}"/>
            </DataTemplate>
          </StackPanel.Resources>
          <ListView Width="400" Height="200" BorderBrush="Navy" BorderThickness="1" x:Name="listView">
            <ListView.View>
              <GridView ColumnHeaderTemplate="{StaticResource myHeaderTemplate}">
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Namespace" DisplayMemberBinding="{Binding Namespace}"/>
                <GridViewColumn Header="FullName" DisplayMemberBinding="{Binding FullName}"/>
              </GridView>
            </ListView.View>
            <x:Type TypeName="Visual"/>
            <x:Type TypeName="UIElement"/>
            <x:Type TypeName="FrameworkElement"/>
          </ListView>
          <Button Width="120" Height="30" Content="SHow" x:Name="btn"/>
        </StackPanel>
      </Page>
    </Window>
    public static class Extensions
    {
        public static T GetVisualChild<T>(this Visual referenceVisual) where T : Visual
        {
            Visual child = null;
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
            {
                child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
                if (child != null && child is T)
                {
                    break;
                }
                else if (child != null)
                {
                    child = GetVisualChild<T>(child);
                    if (child != null && child is T)
                    {
                        break;
                    }
                }
            }
            return child as T;
        }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            btn.Click += delegate
            {
                GridViewHeaderRowPresenter headerRow = listView.GetVisualChild<GridViewHeaderRowPresenter>();
                if (headerRow != null)
                {
                    IEnumerable children = LogicalTreeHelper.GetChildren(headerRow);
                }
            };
        }

        private void GridViewColumnHeader_Checked(Object sender, RoutedEventArgs e)
        {
            ContentPresenter presenter = ((RadioButton)sender).TemplatedParent as ContentPresenter;
            GridViewColumnHeader header = null;
            if (presenter != null)
            {
                header = presenter.TemplatedParent as GridViewColumnHeader;
            }

            GridViewHeaderRowPresenter headerRow = listView.GetVisualChild<GridViewHeaderRowPresenter>();
            if (headerRow != null && header != null)
            {
                foreach (DependencyObject dependencyObject in LogicalTreeHelper.GetChildren(headerRow))
                {
                    GridViewColumnHeader gvch = dependencyObject as GridViewColumnHeader;
                    if(gvch != null && gvch != header)
                    {
                        gvch.Tag = false;
                    }
                }
            }
        }
    }


    Hope this helps

    Monday, March 17, 2008 6:01 AM
  • Hi Marco, Thanks for the reply,

    Unfortunatlly, I'm getting errors trying to implement it.

    The Xaml part  works fine but the code is giving errors in regards to the Extension class(is that the correct syntax

    "public static T GetVisualChild<T>(this Visual referenceVisual) where T : Visual")?

     

    and also in the listview.GetVisualchild the error is "Cannot be used with type arguments".

     

    Thanks

     

     

    Monday, March 17, 2008 8:15 AM
  • Hi Marco,

    I just realized there is another problem. I neglected to mention that in the DataTemplate of the GridViewColumnHeader there is also a checkbox which is already bind to the GridviewColummHeader Tag but for a different property than the one that is related to the radio button, so the Tag is already used:

    <CheckBox IsChecked="{TemplateBinding GridViewColumnHeader.Tag}"/>

     

    so is there a different way than using the tag?

     

    Thanks

     

     

    Monday, March 17, 2008 8:48 AM
  • If the Tag property is already used, then you can choose attached property instead.

    Hope this helps
    Tuesday, March 18, 2008 5:37 AM
  • Ok great Marco,

    But, Unfortunatlly, I'm getting errors trying to implement the code:

    The Xaml part  works fine but the code is giving errors in regards to the Extension class(is that the correct syntax

    "public static T GetVisualChild<T>(this Visual referenceVisual) where T : Visual")?

     

    and also in the listview.GetVisualchild the error is "Cannot be used with type arguments".

     

    Thanks

     

    Tuesday, March 18, 2008 7:23 AM
  • Okay, I guess you are still using Visual Studio 2005, here is the modified code which should work under .NET Framework 3.0:

    public static class VisualHelper
    {
        public static T GetVisualChild<T>(Visual referenceVisual) where T : Visual
        {
            Visual child = null;
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
            {
                child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
                if (child != null && child is T)
                {
                    break;
                }
                else if (child != null)
                {
                    child = GetVisualChild<T>(child);
                    if (child != null && child is T)
                    {
                        break;
                    }
                }
            }
            return child as T;
        }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            btn.Click += delegate
            {
                GridViewHeaderRowPresenter headerRow = listView.GetVisualChild<GridViewHeaderRowPresenter>();
                if (headerRow != null)
                {
                    IEnumerable children = LogicalTreeHelper.GetChildren(headerRow);
                }
            };
        }

        private void GridViewColumnHeader_Checked(Object sender, RoutedEventArgs e)
        {
            ContentPresenter presenter = ((RadioButton)sender).TemplatedParent as ContentPresenter;
            GridViewColumnHeader header = null;
            if (presenter != null)
            {
                header = presenter.TemplatedParent as GridViewColumnHeader;
            }

            GridViewHeaderRowPresenter headerRow = VisualHelper.GetVisualChild<GridViewHeaderRowPresenter>(listView);
            if (headerRow != null && header != null)
            {
                foreach (DependencyObject dependencyObject in LogicalTreeHelper.GetChildren(headerRow))
                {
                    GridViewColumnHeader gvch = dependencyObject as GridViewColumnHeader;
                    if (gvch != null && gvch != header)
                    {
                        gvch.Tag = false;
                    }
                }
            }
        }
    }
    Tuesday, March 18, 2008 8:20 AM
  • Thanks Marco, works like a charm

     

    Wednesday, March 19, 2008 7:30 AM