Search code examples
c#wpflistviewgridviewcolumnframeworkelementfactory

getvalue from FrameworkElementFactory


Am having a list view with one GridViewColumn

        GridViewColumn gvc = new GridViewColumn();

        DataTemplate dt = new DataTemplate();
        FrameworkElementFactory ch = new FrameworkElementFactory(typeof(CheckBox));

        Binding bind= new Binding("Empty");
        ch.SetBinding(CheckBox.IsCheckedProperty, bind);

        dt.VisualTree = ch;
        gvc.CellTemplate = dt;
        (lv.View as GridView).Columns.Add(gvc);

later when I want to retrieve whether the checkbox is checked or not am facing the FrameworkElementFactory, as it has no GetValue method, didn't know ho to cast it to a checkbox, so how can I get the IsChecked property from a FrameworkElementFactory, knowing that am able to access to it for any element of my listview

 ...
var mycheckboxFEF = template.VisualTree.FirstChild;// FirstChild is my FrameWorkElementFactory checkbox
bool isempty= (......) ????

Solution

  • I did provide a solution to your problem through this exemple.
    This should work by selecting an item and pressing the button
    XAML :

    <Button Margin="0,12,401,276" Click="Button_Click">Button</Button>
            <ListView x:Name="yourListView" ItemsSource="{Binding Things}" SelectedItem="{Binding SelectedThing}" Margin="0,41,0,0">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="Check"  Width="250">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox x:Name="myCBox" Content="{Binding ThingName}"></CheckBox>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView> 
    

    Code behind :

     /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                Things = new List<Thing>
                             {
                                 new Thing {ThingName = "t1"},
                                 new Thing {ThingName = "t2"},
                                 new Thing {ThingName = "t4"},
                                 new Thing {ThingName = "t3"},
                             };
                DataContext = this;
    
            }
    
            public List<Thing> Things { get; set; }
            public Thing SelectedThing { get; set; }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var yourListViewItem = (ListViewItem)yourListView.ItemContainerGenerator.ContainerFromItem(yourListView.SelectedItem);
                CheckBox cb = FindByName("myCBox", yourListViewItem) as CheckBox;
                MessageBox.Show(cb.Content + " IsChecked :" + cb.IsChecked);
            }
            private FrameworkElement FindByName(string name, FrameworkElement root)
            {
                Stack<FrameworkElement> tree = new Stack<FrameworkElement>();
                tree.Push(root);
    
                while (tree.Count > 0)
                {
                    FrameworkElement current = tree.Pop();
                    if (current.Name == name)
                        return current;
    
                    int count = VisualTreeHelper.GetChildrenCount(current);
                    for (int i = 0; i < count; ++i)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(current, i);
                        if (child is FrameworkElement)
                            tree.Push((FrameworkElement)child);
                    }
                }
    
                return null;
            }
        }
    
    
    
        public class Thing
        {
            public string ThingName { get; set; }
    
        }
    

    Hope it helps