Click here to Skip to main content
15,881,671 members
Home / Discussions / WPF
   

WPF

 
QuestionMake a 3D casino spin wheel in Silverlight. Pin
Vishal Kumar Singh26-May-14 23:17
Vishal Kumar Singh26-May-14 23:17 
AnswerRe: Make a 3D casino spin wheel in Silverlight. Pin
amigoface27-May-14 2:16
amigoface27-May-14 2:16 
Questionhow to drag and drop with image preview in wpf using c# Pin
kalsa23-May-14 21:20
kalsa23-May-14 21:20 
QuestionBest Practice for using WPF Resources Pin
hari111r19-May-14 19:14
hari111r19-May-14 19:14 
AnswerRe: Best Practice for using WPF Resources Pin
Mycroft Holmes19-May-14 21:22
professionalMycroft Holmes19-May-14 21:22 
GeneralRe: Best Practice for using WPF Resources Pin
hari111r19-May-14 23:52
hari111r19-May-14 23:52 
GeneralRe: Best Practice for using WPF Resources Pin
Mycroft Holmes20-May-14 13:02
professionalMycroft Holmes20-May-14 13:02 
QuestionTwo questions about the TreeView control Pin
Mc_Topaz16-May-14 1:45
Mc_Topaz16-May-14 1:45 
Hi!

I have a WpfApplication with a TreeView and a Button.
XML
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="tvTree">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding List}" DataType="{x:Type local:BaseNode}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Header}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <Button Width="20" Height="20" Content="Click" Click="Button_Click" />
    </Grid>
</Window>

The TreeView's ItemsSource property will be populated from a list based on the BaseNode class. That class look like this:
C#
public class BaseNode
{
    System.Collections.ObjectModel.ObservableCollection<BaseNode> list = new System.Collections.ObjectModel.ObservableCollection<BaseNode>();

    string header;

    public BaseNode(string header)
    {
        this.header = header;
    }

    public string Header
    {
        get { return header; }
        set { header = value; }
    }

    public System.Collections.ObjectModel.ObservableCollection<BaseNode> List
    {
        get { return list; }
        set { list = value; }
    }
}
The MainWindow.cs have this code:
C#
public partial class MainWindow : Window
{
    BaseNode a = new BaseNode("Base");
    BaseNode b = new BaseNode("Sub1");
    BaseNode c = new BaseNode("Sub2");
    BaseNode ba = new BaseNode("Sub1Sub1");
    BaseNode baa = new BaseNode("Sub1Sub1Sub1");

    public MainWindow()
    {
        InitializeComponent();

        a.List.Add(b);
        a.List.Add(c);
        b.List.Add(ba);
        ba.List.Add(baa);
        tvTree.ItemsSource = a.List;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        b.Header = "NewHeader";
    }
}

Question 1:
When I click the button I would like to change the Header property of one of the BaseNodes. But the code for the Button.Click event won't do the trick. Just setting the BaseNode.Header property seem not to be enough. Instead I need to alter the Button.Click callback to:
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
    b.Header = "NewHeader";
    tvTree.ItemsSource = null;
    tvTree.ItemsSource = a.List;
}

This does the job but seem to be very overkill for such a small thing.

Is there a better way to make the TreeView show the new value of the BaseNode.Header property?




Question 2:
Image I have populated the same TreeView with a bunch of BaseNode objects forming a tree with many branches of BaseNodes as well as branches in branches. I lack a way to, manually by code, open or close a branch in the TreeView.

I have found out that this is supported for the TreeViewItem control which have the IsExpanded property. My BaseNode class don't have such a property and including one I highly doubt it will "solve the problem".

With the TreeView.SelectedItemChanged event I can select any BaseNode in the TreeView and find out the actual BaseNode instance I selected. From this point I would like to be able to open or close the nested BaseNodes.

How to open or close a nested node in the TreeView which is not a TreeViewItem?

Best regards
/Mc_Topaz
AnswerRe: Two questions about the TreeView control Pin
SledgeHammer0116-May-14 6:45
SledgeHammer0116-May-14 6:45 
QuestionRe: Two questions about the TreeView control Pin
Mc_Topaz16-May-14 7:40
Mc_Topaz16-May-14 7:40 
AnswerRe: Two questions about the TreeView control Pin
SledgeHammer0116-May-14 8:16
SledgeHammer0116-May-14 8:16 
GeneralRe: Two questions about the TreeView control Pin
Mc_Topaz16-May-14 9:08
Mc_Topaz16-May-14 9:08 
QuestionOCR using Microsoft Office Document Imaging 12.0 Type Library in WPF application Pin
Member 785476914-May-14 20:20
Member 785476914-May-14 20:20 
AnswerRe: OCR using Microsoft Office Document Imaging 12.0 Type Library in WPF application Pin
AlphaDeltaTheta15-May-14 16:48
AlphaDeltaTheta15-May-14 16:48 
QuestionSilverlight: Exception with ListBox and nested collection Pin
tongo7612-May-14 23:01
tongo7612-May-14 23:01 
AnswerRe: Silverlight: Exception with ListBox and nested collection Pin
Mycroft Holmes13-May-14 12:56
professionalMycroft Holmes13-May-14 12:56 
QuestionWPF Generate Dynamic Messages Including Links Pin
Kevin Marois6-May-14 16:44
professionalKevin Marois6-May-14 16:44 
AnswerRe: WPF Generate Dynamic Messages Including Links Pin
Bernhard Hiller6-May-14 20:58
Bernhard Hiller6-May-14 20:58 
QuestionCreating multilanguage WPF MVVM application Pin
sifi mohamed amine2-May-14 4:43
sifi mohamed amine2-May-14 4:43 
QuestionMultiBinding Question Pin
Kevin Marois1-May-14 14:32
professionalKevin Marois1-May-14 14:32 
QuestionUpdate Instance Property From DP Pin
Kevin Marois29-Apr-14 6:29
professionalKevin Marois29-Apr-14 6:29 
AnswerRe: Update Instance Property From DP Pin
Richard Deeming29-Apr-14 6:34
mveRichard Deeming29-Apr-14 6:34 
GeneralRe: Update Instance Property From DP Pin
Kevin Marois29-Apr-14 6:37
professionalKevin Marois29-Apr-14 6:37 
QuestionWPF Style Question Pin
Kevin Marois28-Apr-14 7:18
professionalKevin Marois28-Apr-14 7:18 
AnswerRe: WPF Style Question Pin
Pete O'Hanlon28-Apr-14 7:24
mvePete O'Hanlon28-Apr-14 7:24 

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.