tags:

views:

32

answers:

2

I have a public CLR property defined in the code behind of a WPF Window. This property only has a setter defined.

public SomeCustomType SomeProperty {
    set {
        someValue = value;
    }
}

I also have a ComboBox defined in the XAML of this WPF Window. This ComboBox has two ComboBoxItem objects defined. The Content properties of the ComboBoxItem objects are set to the text that I want to display for those items. The Tag properties of the ComboBoxItem objects are set to a static CLR property on another class. The SelectedValuePath is set to "Tag" on the ComboBox. I have the SelectedValue bound to the Window's CLR property with the mode set to OneWayToSource and the UpdateSourceTrigger set to PropertyChanged.

<ComboBox SelectedValue="{Binding Registry, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Tag" SelectedIndex="0">
    <ComboBoxItem Content="Item1" Tag={x:Static someNamespace:SomeType.SomeStaticCLRProperty}" />
    <ComboBoxItem Content="Item1" Tag={x:Static someNamespace:SomeType.SomeStaticCLRProperty}" />
</ComboBox>

What I want to have happen is: when an item is selected in the ComboBox, the CLR property in the Window code behind should be set to the selected value. I do not want the code behind to ever set the value of the ComboBox.

But when I run this, I am getting an exception: "Property Get method was not found.". The way I read this, it is complaining that I don't have a getter defined on the Window's CLR property. But I don't think I should need one.

Am I doing something obviously wrong here?

Thanks!

EDIT: I should mention that adding the getter does make it work, but the getter is getting hit whenever the setter does. Also, the first time the setter is called (when the Window loads), the value is null. But then it doesn't get called again until the user selects something. Shouldn't I be getting a valid value from the selected value the first time around?

A: 

I'm not entirely sure what the problem is, but you can definitely OneWayToSource bind without a getter. I think the problem has something to do with the fact that your binding a Tag (of type object) to a property of SomeCustomType. You may have to use a ValueConverter to get that to work.

mdm20
Hey there. Thanks for your answer. I changed the property to one of type 'object', then did an 'as' dynamic cast to my internal variable. This should resolve the issue that you proposed, no? Unfortunately, the exception still presents itself.
chrensli
Can you post the full class you are using and the actual XAML?
mdm20
It is a pretty large class, but more importantly, I am working under NDA, so I cannot post the full class. However, the above answer (sanity check) seems to work for me too, so I guess I at least have something to work from now. Thanks again for your help.
chrensli
+1  A: 

I tried something out and the code below works. I hope this helps:

Code-Behind:


public partial class MainWindow : Window
    {
        private SomeCustomType registry;
        public SomeCustomType Registry { set { registry = value; } }

        public MainWindow()
        {
            InitializeComponent();
            this.comboBox.DataContext = this;
        }

    }

    public class SomeType
    {
        public static SomeCustomType Property1 { get { return new SomeCustomType() { Name = "Item1" }; } }
        public static SomeCustomType Property2 { get { return new SomeCustomType() { Name = "Item2" }; } }
    }

    public class SomeCustomType
    {
        public string Name { get; set; }
    }

XAML:

<ComboBox x:Name="comboBox" SelectedValue="{Binding Registry, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" 
              SelectedValuePath="Tag" SelectedIndex="0">
    <ComboBoxItem Content="Item1" Tag="{x:Static local:SomeType.Property1}" />
    <ComboBoxItem Content="Item2" Tag="{x:Static local:SomeType.Property2}" />
</ComboBox>
karmicpuppet
Indeed, this works for me too. Now to figure out what I did differently! Thanks for the reminder that a simple sanity check is sometimes all that is needed to answer my own question.
chrensli
In case anyone comes here looking for an answer to my original question, I had Break On Exception enabled in my debugging options. An exception is thrown in this code and my own code, but it is not unhandled. I've read elsewhere since I asked this question that it does in fact access the getter, despite what you would think. It doesn't seem to use the value though, so it isn't anything to worry about. Strange...
chrensli