tags:

views:

25

answers:

2

I would like to synchronize two object headers with pointers. For example: I know I can't do this with the current syntax, but I would like something like:

Node node = new Node();
node.Label = "header1";

TabItem tabItem = new TabItem;
*(tabItem.Header) = &(node.Label);

So whenever I change node.Label, tabItem.Header changes as well.

+2  A: 

Edit: added an example

You should be able to accomplish this using databinding. If Node is a custom object, you will want to either suport INotifyPropertyChange, or implement the Label property as a dependency property. (I'm not sure what the convention is, I would guess that if Node is intrinsically a UI object, then use a dependency property, and if it's not then implement INotifyPropertyChanged.

Example: The Node object:


    public class Node: System.ComponentModel.INotifyPropertyChanged
    {
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        public string Label
        {
            get { return this._Label; }
            set
            {
                this._Label = value;
                this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Label"));
            }
        }
        private string _Label;

        protected virtual void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs propertyChangedEventArgs)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if (propertyChanged != null)
                propertyChanged(this, propertyChangedEventArgs);
        }
    }

The code behind your form (this method sets up your databinding in code, you could also set up the databinding in the XAML):


    public partial class MainWindow : Window
    {
        public Node TheNode { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            this.TheNode = new Node();
            this.DataContext = this;
            this.Tab1.SetBinding(System.Windows.Controls.TabItem.HeaderProperty, "TheNode.Label");
            this.TheNode.Label = "Test";
        }
    }

The XAML for your form

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TabControl>
        <TabItem Name="Tab1"/>
    </TabControl>
</Grid>

When you databind, WPF will detect that your Node object implements INotifyPropertyChanged, and it will subscribe the PropertyChanged event automatically.
(notice that in the MainWindow code, I set the binding and then I set the value of the Node.Label -- the tab header detects it and updates).

JMarsch
Thank you! It's exactly what I needed
anon
A: 

Have you tried to create a new Binding to bind the two properties? If Node is your own object, implement INotifyPropertyChanged. You can do it in code as well as XAML. Since you look like you are doing things within code, here is a link on how to do that: http://learnwpf.com/Posts/Post.aspx?postId=a089e315-9440-4f74-b872-ba6d14be7c80

Jeremiah Morrill