(WPF)How to reference an object created in the code behind from XAML

M

mick

Been struggling with this for ages so you are my last resort before my head
explodes -

Given this code behind file below how would I reference the myColors object
in the XAML file
for binding purposes, something along he lines of -

<Rectangle Fill="{Binding Source=myColors2, Path=SelectedColorString}" ....>

I'm a beginner with WPF so simple as possible please:)




//--------- Code behind ----------------

namespace BindingExamples
{
public partial class MainWindow : Window
{
MyColors myColors = new MyColors();

public MainWindow()
{
InitializeComponent();
}
}

public class MyColors:DependencyObject
{
public static readonly DependencyProperty
SelectedColorStringProperty =
DependencyProperty.Register("SelectedColorString",
typeof(string), typeof(MyColors));

public string SelectedColorString
{
get
{
return (string)GetValue(SelectedColorStringProperty);
}
set
{
SetValue(SelectedColorStringProperty, value);
}
}

public MyColors(){}
}
}



mick
 
A

andy

Been struggling with this for ages so you are my last resort before my head
explodes -

Given  this code behind file below how would I reference the myColors object
in the XAML file
for binding purposes, something along he lines of -

<Rectangle Fill="{Binding Source=myColors2, Path=SelectedColorString}" ....>

I'm a beginner with WPF so simple as possible please:)

//--------- Code behind ----------------

namespace BindingExamples
{
    public partial class MainWindow : Window
    {
        MyColors myColors = new MyColors();

        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MyColors:DependencyObject
    {
        public static readonly DependencyProperty
SelectedColorStringProperty =
           DependencyProperty.Register("SelectedColorString",
typeof(string), typeof(MyColors));

        public string SelectedColorString
        {
            get
            {
                return (string)GetValue(SelectedColorStringProperty);
            }
            set
            {
                SetValue(SelectedColorStringProperty, value);
            }
        }

        public MyColors(){}
    }

}

mick

Hi Mick,

You want to ask WPF questions over on the wpf web forum
http://social.msdn.microsoft.com/Forums/en-US/wpf/threads
Much more WPF traffic there.

SImplest way is to inject the instance of the object in the
constructor.
Something very roughly along the lines of:

public partial class MainWindow : Window
{
MyColors myColors = new MyColors();
public MainWindow()
{
InitializeComponent();
this.whatever.datacontext = MyColors
}
}


But before you do this you want to take a long look at MVVM.
Yes there's a learning curve and yes if you're just used to sticking
code in button click events it will look harder at first.
MVVM will save you a whole world of pain later on.

Follow the advice here, where I'm Andy1559
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2bade6d3-0d14-4492-b2be-cbc7ca416506/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top