数据绑定TreeView与CheckBoxes WPF

时间:2014-05-12 06:44:22

标签: c# wpf data-binding checkbox treeview

我有一个窗口

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:GRHelper="clr-namespace:Enertek.GRHelper;assembly=Enertek.GRHelper" xmlns:local="clr-namespace:EneGR" x:Class="EneGR.SearchWindow"
        Title="Main Window" Height="274.753" Width="322.345" Icon="icon.ico">
    <Window.OpacityMask>
        <RadialGradientBrush>
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="White" Offset="1"/>
        </RadialGradientBrush>
    </Window.OpacityMask>
    <Window.BorderBrush>
        <ImageBrush Stretch="None" TileMode="FlipXY"/>
    </Window.BorderBrush>

    <Grid RenderTransformOrigin="0.5,0.5" Margin="0,0,2,6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto" MinWidth="120"/>
            <ColumnDefinition Width="65"/>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="70"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="SearchButton" Content="Search" Grid.Column="3" Margin="0,62,0,0" VerticalAlignment="Top" Height="23" RenderTransformOrigin="0.6,0.458" FontWeight="Bold"
            Click="SearchButton_Click"/>
        <Label x:Name="SearchLabel" Content="Enter object's name or its part in the field below:" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Grid.ColumnSpan="4" Width="285" Height="36"/>
        <Button x:Name="SearchCancel" Grid.ColumnSpan="2" Content="Cancel" HorizontalAlignment="Left" Margin="0,61,0,0" VerticalAlignment="Top" Width="120" Height="24" FontWeight="Bold"
            Click=" SearchCancel_Click"/>
        <TreeView  Grid.Column ="1" ItemsSource="{Binding objs}" Margin="0,101,3,40" Grid.ColumnSpan="3" Background="White">

            <TreeView.DataContext>
                <local:MyTreeData/>
            </TreeView.DataContext>
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Checked, Mode=TwoWay}" Content="{Binding TagName}" Checked="CheckBox_Checked"/>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>


        <Button x:Name="Rename" Content="Rename" Grid.Column="2" HorizontalAlignment="Left" Margin="62,212,0,0" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Click="Rename_Click" FontWeight="Bold"/>
        <TextBox x:Name="TagNameBox" Grid.ColumnSpan="3" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="10,33,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="285"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="100" Margin="-106,257,0,-115" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

背后的代码:

namespace EneGR
{

    public class MyTreeData
    {
        public ObservableCollection<GRObject> objs { get; set; }
        private bool allAreChecked = false;

        public MyTreeData() 
        {
            objs = new ObservableCollection<GRObject>();

        }

        public bool AllAreChecked 
        {
            get 
            {
                return allAreChecked;
            }

            set 
            {
                allAreChecked = value;

            }
        }

    }

    /// <summary>
    /// Interaction logic for SearchWindow.xaml
    /// </summary>
    public partial class SearchWindow : Window
    {
        GRGalaxy galaxy = EneGR.LoginWin.galaxy;
        List <GRObject> ObjsToRename = null;
        public MyTreeData TreeData = new MyTreeData();  

        public SearchWindow()
        {
            InitializeComponent();

            DataContext = TreeData;
        }

        private void SearchCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            string TagName = TagNameBox.Text;

            IEnumerable<GRObject> objects = galaxy.QueryObjectsByName(TagName + '%');
            MessageBox.Show(objects.Count().ToString() + " " + "Objects found" );

            foreach (GRObject obj in objects) 
            {   
               TreeData.objs.Add(obj);
            };
        }


        private void Rename_Click(object sender, RoutedEventArgs e)
        {

            galaxy.RenameCheckedObjects(ObjsToRename);
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
           //add checked object to ienumerable collection ObjsToRename;
        }


    }
}

我将来自可观察集合TreeData.objs的对象绑定到带有CheckBoxes的TreeView,但它们从未显示在窗口中。我无法理解有什么不对。 IEnumerable对象不为null。

1 个答案:

答案 0 :(得分:0)

您可以删除此代码:

   <TreeView.DataContext>
        <local:MyTreeData/>
    </TreeView.DataContext>

...创建MyTreeData的新实例并将TreeView的DataContext设置为它。

您的树视图的上下文已设置为您要使用的MyTreeData(因为DataContext继承):

   public SearchWindow()
    {
        InitializeComponent();
        DataContext = TreeData;
    }