Click here to Skip to main content
15,895,799 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Installation Requirements Pin
Pete O'Hanlon14-Aug-11 8:03
mvePete O'Hanlon14-Aug-11 8:03 
GeneralRe: Installation Requirements Pin
arkiboys14-Aug-11 8:57
arkiboys14-Aug-11 8:57 
GeneralRe: Installation Requirements Pin
Richard MacCutchan14-Aug-11 8:04
mveRichard MacCutchan14-Aug-11 8:04 
GeneralRe: Installation Requirements Pin
arkiboys14-Aug-11 8:57
arkiboys14-Aug-11 8:57 
QuestionBinding in two way mode from a XML file to a combobox in usercontrol [modified] Pin
npuleio13-Aug-11 3:57
npuleio13-Aug-11 3:57 
QuestionAccessing function on Usercontrol hosted from Window in MVVM and C# Pin
Alisaunder12-Aug-11 5:55
Alisaunder12-Aug-11 5:55 
AnswerRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0112-Aug-11 6:52
SledgeHammer0112-Aug-11 6:52 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# [modified] Pin
Alisaunder12-Aug-11 7:25
Alisaunder12-Aug-11 7:25 
How do I populate that type of Treeview? I've created a listview such like you said for my MRU list I need to populate the Treeview with sorted data by Alphabetic base letter A-Z then Lastname, Firstname middle intial. I'm already using a collection:

C#
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Manufactured_Housing_Manager
{
    public class tvIndex
    {
        private string _index;
        public string Index
        {
            get { return _index; }
            set { _index = value; }
        }

        private ObservableCollection<Contact> _contacts;
        public ObservableCollection<Contact> Contacts
        {
            get
            {
                if (_contacts == null)
                    _contacts = new ObservableCollection<Contact>();

                return _contacts;
            }
        }

        public tvIndex()
        {
        }

        public tvIndex(
            string indexx,
            ObservableCollection<Contact> contacts)
        {
            _index = indexx;
            _contacts = contacts;
        }
    }
}

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Manufactured_Housing_Manager
{
    public class Contact
    {
        private string _name;
        private int _id;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public Contact()
        {
        }

        public Contact(string name, int id)
        {
            _name = name;
            _id = id;
        }
    }
}


My Xaml is this:

HTML
<UserControl x:Class="Manufactured_Housing_Manager.Outlookbar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Manufactured_Housing_Manager"
             mc:Ignorable="d" 
             >
    <UserControl.Resources>
        <Style x:Key="TreeViewStyle1" TargetType="{x:Type TreeView}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeView}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
                            <ScrollViewer x:Name="_tv_scrollviewer_" Background="{TemplateBinding Background}" CanContentScroll="True" Focusable="false" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" Template="{DynamicResource ScrollViewerControlTemplate1}" >
                                <ItemsPresenter ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True"/>
                            </ScrollViewer>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                                <Setter Property="CanContentScroll" TargetName="_tv_scrollviewer_" Value="true"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

        <!--========================== OutlookBar Treeview Style ===========================-->


        <HierarchicalDataTemplate x:Key="ChildTemplate">
            <StackPanel Orientation="Horizontal">
                <Image x:Name="img2" Width="16"	Height="16"	Stretch="Fill" Source="Images\closedfolder16.png" />
                <TextBlock Text="{Binding Path=Name}" Margin="5,0" ToolTip="{Binding Path=Name}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="MyTreeViewStyle"
                                  ItemsSource="{Binding Path=Contacts}"
                                  ItemTemplate="{StaticResource ChildTemplate}">
            <!-- Display the Index by showing it's Index string -->
            <StackPanel Orientation="Horizontal">
                <Image x:Name="img" Width="16" Height="16" Stretch="Fill" Source="Images\closedfolder16.png" />
                <TextBlock Text="{Binding Path=Index}" Margin="5,0" ToolTip="{Binding Path=Index}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
    </UserControl.Resources>

    <DockPanel>
        <Grid>
            <Border Margin="5,5,0,0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="lightGray" CornerRadius="3" Height="24" Width="Auto" VerticalAlignment="Top" HorizontalAlignment="Stretch">
                <TextBlock Margin ="5,0" Name="tbOutlookBar" VerticalAlignment="Center" Text="Contacts" Foreground="#FF355AAF" FontSize="14" />
            </Border>
            <Border Name="OutlookBarInfo" DockPanel.Dock="Left" Margin="5,28,0,0" Background="White" Height="24" Width="Auto" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="1" BorderBrush="lightGray" CornerRadius="3">
                <TextBlock Margin ="5,0" Name="tbOutlookBarInfo" VerticalAlignment="Center" Text=" " Foreground="Black" FontSize="12" />
            </Border>
            <TabControl Margin="5,52,0,5" Grid.RowSpan="2" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" TabStripPlacement="Bottom" Style="{DynamicResource OutlookTabControlStyle}" >
                <TabItem Height="24" Style="{DynamicResource OutlookTabItemStyle}" Background="{DynamicResource OutlookButtonBackground}" Foreground="{DynamicResource OutlookButtonForeground}" ToolTip="Contacts">
                    <!--GotFocus="TabItemContacts_GotFocus">-->
                    <TabItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <Image Name="img" Source="Images/contact16.png" Stretch="Fill" Height="16" Width="16" Margin="0,3" />
                            <TextBlock Text="Contacts" Margin="3,0,0,0" VerticalAlignment="Center" />
                        </StackPanel>
                    </TabItem.Header>
                    <Grid>
                        <TreeView ItemsSource="{x:Static local:RibbonControl.contactdata}" x:Name="MyTreeView" ItemTemplate="{StaticResource MyTreeViewStyle}" ScrollViewer.CanContentScroll="True" Style="{DynamicResource TreeViewStyle1}">
                            <!-- TreeViewItem.Selected="MyTreeView_Selected">-->
                            <TreeView.ItemContainerStyle>
                                <Style TargetType="{x:Type TreeViewItem}">
                                    <Setter Property="IsExpanded" Value="True" />
                                </Style>
                            </TreeView.ItemContainerStyle>
                        </TreeView>
                    </Grid>
                </TabItem>
                <TabItem Grid.ColumnSpan="2" Height="24" Style="{DynamicResource OutlookTabItemStyle}" Background="{DynamicResource OutlookButtonBackground}" Foreground="{DynamicResource OutlookButtonForeground}" ToolTip="Calendar">
                    <!-- GotFocus="TabItemCalendar_GotFocus" LostFocus="TabItem_LostFocus">-->
                    <TabItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <Image Name="img2" Source="Images/calendar16.png" Stretch="Fill" Height="16" Width="16"  Margin="0,3" />
                            <TextBlock Text="Calendar" Margin="3,0,0,0" VerticalAlignment="Center" />
                        </StackPanel>
                    </TabItem.Header>
                    <Grid>
                        <!--<Calendar Name="calendar1" HorizontalContentAlignment="Stretch" d:LayoutOverrides="GridBox" Height="170" VerticalAlignment="Top" />-->
                    </Grid>
                </TabItem>
            </TabControl>
        </Grid>
    </DockPanel>
</UserControl>


almost forgot the code inside the User Control:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Manufactured_Housing_Manager;

namespace Manufactured_Housing_Manager
{
    /// <summary>
    /// Interaction logic for Outlookbar.xaml
    /// </summary>
    public partial class Outlookbar : UserControl
    {
        public DataTable dtContacts = new DataTable();
        public DataTable dtDealerPacks = new DataTable();
        public DataTable dtServices = new DataTable();
        public DataRow rowContacts;
        public DataRow rowDealerPacks;
        public DataRow rowServices;
        public string filename;
        public string connectionString;
        static public ObservableCollection<Contact> contactdata;

        public Outlookbar()
        {
            InitializeComponent();
        }

        #region Update TreeView

        // TreeView Control
        private void updateTreeview()
        {
            //MyTreeView.Items.Clear();

            if (Properties.Settings.Default.dbOpen)
            {
                //add letters sort nodes
                for (int i = System.Convert.ToInt32('A'); i <= System.Convert.ToInt32('Z'); i++)
                {
                    MyTreeView.Items.Add(new tvIndex(Convert.ToString((char)(i)), contactdata)); //set text

                    //add customers                    
                    contactdata = new ObservableCollection<Contact>();
                    string sContactsName = null;
                    foreach (DataRow rowContacts in Properties.Settings.Default.DtContacts.Rows)
                    {
                        sContactsName = string.Concat(rowContacts["LastName"].ToString(), ", ", rowContacts["FirstName"].ToString());

                        if (!(System.Convert.IsDBNull(rowContacts["MiddleInitial"].ToString())))
                        {
                            if (!(rowContacts["MiddleInitial"].ToString() == ""))
                                sContactsName = string.Concat(sContactsName, " ", rowContacts["MiddleInitial"].ToString(), ".");
                        }

                        if (left(sContactsName, 1) == Convert.ToString((char)(i + 1)))
                        {
                            string temp = rowContacts["ContactID"].ToString();
                            contactdata.Add(new Contact(sContactsName, Convert.ToInt32(temp)));
                        }
                    }
                }

                if (Properties.Settings.Default.DtContacts.Rows.Count < 2)
                {
                    tbOutlookBarInfo.Text = Properties.Settings.Default.DtContacts.Rows.Count + " Contact";
                    tbOutlookBarInfo.ToolTip = Properties.Settings.Default.DtContacts.Rows.Count + " Contact";
                }
                else
                {
                    tbOutlookBarInfo.Text = Properties.Settings.Default.DtContacts.Rows.Count + " Contacts";
                    tbOutlookBarInfo.ToolTip = Properties.Settings.Default.DtContacts.Rows.Count + " Contacts";
                }
            }
        }

        #endregion
    }
}


modified on Friday, August 12, 2011 1:36 PM

GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0112-Aug-11 7:42
SledgeHammer0112-Aug-11 7:42 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
Alisaunder12-Aug-11 8:12
Alisaunder12-Aug-11 8:12 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0112-Aug-11 8:38
SledgeHammer0112-Aug-11 8:38 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# [modified] Pin
Alisaunder12-Aug-11 10:26
Alisaunder12-Aug-11 10:26 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0112-Aug-11 11:01
SledgeHammer0112-Aug-11 11:01 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# [modified] Pin
Alisaunder12-Aug-11 13:32
Alisaunder12-Aug-11 13:32 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
Alisaunder13-Aug-11 11:25
Alisaunder13-Aug-11 11:25 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0113-Aug-11 11:49
SledgeHammer0113-Aug-11 11:49 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
Alisaunder13-Aug-11 15:09
Alisaunder13-Aug-11 15:09 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0113-Aug-11 15:35
SledgeHammer0113-Aug-11 15:35 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
Alisaunder14-Aug-11 14:36
Alisaunder14-Aug-11 14:36 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
SledgeHammer0114-Aug-11 15:04
SledgeHammer0114-Aug-11 15:04 
GeneralRe: Accessing function on Usercontrol hosted from Window in MVVM and C# Pin
Alisaunder15-Aug-11 22:01
Alisaunder15-Aug-11 22:01 
Questionsiilverlight 4 - listbox to have Tag Pin
arkiboys12-Aug-11 4:32
arkiboys12-Aug-11 4:32 
AnswerRe: siilverlight 4 - listbox to have Tag Pin
#realJSOP12-Aug-11 6:32
mve#realJSOP12-Aug-11 6:32 
Questionsilverlight listbox - fill colour Pin
arkiboys12-Aug-11 4:24
arkiboys12-Aug-11 4:24 
AnswerRe: silverlight listbox - fill colour Pin
Mycroft Holmes12-Aug-11 23:44
professionalMycroft Holmes12-Aug-11 23:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.