Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从FrameworkElementFactory获取值_C#_Wpf_Listview_Gridviewcolumn_Frameworkelementfactory - Fatal编程技术网

C# 从FrameworkElementFactory获取值

C# 从FrameworkElementFactory获取值,c#,wpf,listview,gridviewcolumn,frameworkelementfactory,C#,Wpf,Listview,Gridviewcolumn,Frameworkelementfactory,我有一个带有一个GridViewColumn的列表视图 GridViewColumn gvc = new GridViewColumn(); DataTemplate dt = new DataTemplate(); FrameworkElementFactory ch = new FrameworkElementFactory(typeof(CheckBox)); Binding bind= new Binding("Empt

我有一个带有一个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);
稍后,当我想检索复选框是否选中时,我面对的是FrameworkElementFactory,因为它没有GetValue方法,不知道如何将其强制转换为复选框,因此如何从FrameworkElementFactory获取IsChecked属性,知道我可以访问listview中的任何元素

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

我确实通过这个例子为您的问题提供了解决方案。
这应该通过选择一个项目并按下按钮
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> 
按钮
代码隐藏:

 /// <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; }

    }
//
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
事物=新列表
{
新事物{ThingName=“t1”},
新事物{ThingName=“t2”},
新事物{ThingName=“t4”},
新事物{ThingName=“t3”},
};
DataContext=this;
}
公共列表事物{get;set;}
公共事物已选择内容{get;set;}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var yourListViewItem=(ListViewItem)yourListView.ItemContainerGenerator.ContainerFromItem(yourListView.SelectedItem);
复选框cb=FindByName(“myCBox”,yourListViewItem)作为复选框;
MessageBox.Show(cb.Content+“IsChecked:”+cb.IsChecked);
}
私有FrameworkElement FindByName(字符串名称,FrameworkElement根)
{
堆栈树=新堆栈();
树。推(根);
而(tree.Count>0)
{
FrameworkElement当前=tree.Pop();
if(current.Name==Name)
回流;
int count=VisualTreeHelper.GetChildrenCount(当前);
对于(int i=0;i

希望有帮助

listview是否通过ItemsSource将集合绑定到它?是的,我的listview将集合绑定到它的
ItemsSource
那么,为什么您不能从集合中检索使您的
复选框
处于选中/未选中状态的值?我只绑定了CheckBox.Text属性