WPF Binding, OneWayToSource, "Property acquisition method not found."

I have a public CLR property defined in the code outside the WPF window. This property has only a specific setter.

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. The properties of the contents of the ComboBoxItem objects are set to the text that I want to display for these elements. The properties of the ComboBoxItem object tag are set to the static CLR property in another class. SelectedValuePath is set to Tag on the ComboBox. I have a SelectedValue property associated with the Window CLR property, with the mode set to OneWayToSource, and 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>

, : ComboBox, CLR . , - ComboBox.

, : " Property Get ". , , , , Window CLR. , .

- ?

!

EDIT: , , , . , ( ) null. , . ?

0
4

-, . , :

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>
+1

, , , , . , , , , . , .

+1

I'm not quite sure what the problem is, but you can definitely bind OneWayToSource without a getter. I think the problem is that the tag (type object) is bound to the SomeCustomType property. You may need to use ValueConverter to get this to work.

0
source

Set getter and setter base class properties for protection.

0
source

Source: https://habr.com/ru/post/1545917/


All Articles