首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自FrameworkElementFactory的getvalue

来自FrameworkElementFactory的getvalue
EN

Stack Overflow用户
提问于 2012-09-15 01:30:45
回答 1查看 2.4K关注 0票数 2

具有一个GridViewColumn的列表视图。

代码语言:javascript
复制
        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,方法,所以不知道何去何从将其转换为复选框,那么如何从FrameworkElementFactory,中获取IsChecked属性,知道它能够访问列表视图中的任何元素

代码语言:javascript
复制
 ...
var mycheckboxFEF = template.VisualTree.FirstChild;// FirstChild is my FrameWorkElementFactory checkbox
bool isempty= (......) ????
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-16 15:59:54

通过这个例子,我确实为你的问题提供了一个解决方案。

这应该通过选择一项并按下按钮来完成。

XAML:

代码语言:javascript
复制
<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> 

代码背后:

代码语言:javascript
复制
 /// <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; }

    }

希望它能帮上忙

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12433806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档