tags:

views:

474

answers:

2

hi everyone! i am new in wpf so forgive me i want more!!! i am trying to build an application. i have a treeview that bounded a datasource. it is okay. i have two problems. First; how can i select an item from treeview? i wanna a new window popsup when i select an item; when i click another item; another window popsup. Second problem is that i can not change foreground and font.Thanks everyone in advance

namespace CellBiology { public partial class TreeView { public TreeView() { this.InitializeComponent(); BindTreeView();

 }

    public void BindTreeView()
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\CellBiology.mdb;Persist Security Info=True");

        try
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Select * from Topics", con);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Topics");
            int row = ds.Tables["Topics"].Rows.Count;
            List<MyMenuItem> myList = new List<MyMenuItem>();


            if (row > 0)
            {

                for (int i = 0; i <= row - 1; i++)
                {
                    myList.Add(new MyMenuItem(Convert.ToInt32(ds.Tables["Topics"].Rows[i][0].ToString()), ds.Tables["Topics"].Rows[i][1].ToString(), 0));

                    for (int j = 0; j <= row - 1; j++)
                    {

                        if (ds.Tables["Topics"].Rows[i][0].ToString() == ds.Tables["Topics"].Rows[j][2].ToString())
                        {

                            myList.Add(new MyMenuItem(Convert.ToInt32(ds.Tables["Topics"].Rows[j][0].ToString()), ds.Tables["Topics"].Rows[j][1].ToString(), Convert.ToInt32(ds.Tables["Topics"].Rows[i][0].ToString())));



                            for (int k = 0; k <= row - 1; k++)
                            {

                                if (ds.Tables["Topics"].Rows[j][0].ToString() == ds.Tables["Topics"].Rows[k][2].ToString())
                                {

                                    myList.Add(new MyMenuItem(Convert.ToInt32(ds.Tables["Topics"].Rows[k][0].ToString()),
                                    ds.Tables["Topics"].Rows[k][1].ToString(),
                                    Convert.ToInt32(ds.Tables["Topics"].Rows[j][0].ToString())));


                                    for (int l = 0; l <= row - 1; l++)


                                        if (ds.Tables["Topics"].Rows[k][0].ToString() == ds.Tables["Topics"].Rows[l][2].ToString())
                                        {

                                            myList.Add(new MyMenuItem(Convert.ToInt32(ds.Tables["Topics"].Rows[l][0].ToString()),
                                                ds.Tables["Topics"].Rows[l][1].ToString(), Convert.ToInt32(ds.Tables["Topics"].Rows[k][0].ToString())));





                Dictionary<int, TreeViewItem> flattenedTree = new Dictionary<int, TreeViewItem>();
                foreach (MyMenuItem item in myList)
                {

                    TreeViewItem treenode = new TreeViewItem();
                    treenode.Header = item.TopicName;
                    treenode.Tag = item;
                    flattenedTree.Add(item.TopicID, treenode);
                    if (flattenedTree.ContainsKey(item.TopLevelID))
                    {


                        flattenedTree[item.TopLevelID].Items.Add(treenode);




                    }
                    else
                    {

                        myTreeView.Items.Add(treenode);





    }
    public class MyMenuItem
    {

        internal int TopicID { get; set; }
        internal string TopicName { get; set; }
        internal int TopLevelID { get; set; }


        internal MyMenuItem(int topicid, string topicname, int toplevelid)
        {

            TopicID = topicid;
            TopicName = topicname;
            TopLevelID = toplevelid;




    private void myTreeView_SelectedItemChanged(object sender, RoutedEventArgs e)
    {

        **how  can i code here?**




    }

}

}

A: 

To answer your "how can i code here?" specifically, you can do the following to access the TreeViewItem:

TreeViewItem selectedTreeViewItem = ((TreeViewItem)e.NewValue);

Once you've got that you can access the data that that item represents via it's DataContext property like so:

MyDataType myData = (MyDataType)selectedTreeViewItem.DataContext;

To popup the new window you can create an instance of your Window sublass and either use ShowDialog, if you want it to be modal, or Show if you want to allow mulitple windows to be opened at the same time.

Drew Marsh
thanks for yor comment. But i couldnt make it work? at where am i doing wrong?
neki284
A: 

To change the font:

myWindow.FontFamily = new FontFamily("Font Name");

Where the "Font Name" is the name of known font types, eg. "Times New Roman" or "Comic Sans MS". You can find more usages here.

To Change the foreground color:

myWindow.Foreground = new SolidColorBrush(Colors.Red);

Where Colors.Red is can be any 'pre-defined' color of your choice.

Hope it helps.

Tri Q