0

I have a collection:

public static readonly DependencyProperty UserAutoCalculationTemplatesProperty = DependencyProperty.Register("UserAutoCalculationTemplates", typeof(ObservableCollection<AutoCalculationTemplate>), typeof(Options));
public ObservableCollection<AutoCalculationTemplate> UserAutoCalculationTemplates
{
    get => (ObservableCollection<AutoCalculationTemplate>)GetValue(UserAutoCalculationTemplatesProperty);
    set => SetValue(UserAutoCalculationTemplatesProperty, value);
}

AutoCalculationTemplate Class:

public class AutoCalculationTemplate : INotifyPropertyChanged
{
    private CalculationVariants _templateType;

    public CalculationVariants TemplateType
    {
        get
        {
            return _templateType;

        }
        set
        {
            _templateType = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TemplateType"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, e);
    }
}

My XAML.

<DataGrid ItemsSource="{Binding UserAutoCalculationTemplates, ElementName=OptionsWindow,Mode=TwoWay,NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" Name="AutoCalculaionTemplates">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <ComboBox
                                            x:Name="AutoCalculationTypeCombo"
                                            ItemsSource="{Binding Source={enum:Enumeration {x:Type options:CalculationVariants}}}"
                                            DisplayMemberPath="Description"
                                            SelectedValue="{Binding UserAutoCalculationTemplates.TemplateType, ElementName=OptionsWindow,Mode=TwoWay,NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                                            SelectedValuePath="Value"
                                            DropDownClosed="AutoCalculationTypeCombo_OnDropDownClosed"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                        </DataGrid.Columns>
                    </DataGrid>

Combobox bound to enum via extension from this topic: Databinding an enum property to a ComboBox in WPF:

enum:

public enum CalculationVariants
{
    [Description("Внутренний")]
    CalculateWithCSharp,
    [Description("Внешний скрипт")]
    CalculateWithPython,
    [Description("Общий")]
    Common
}

I want Combobox to do two things:

  1. display all possible values of enum (their descriptions) on click;
  2. display current value of TemplateType propertie of items in UserAutoCalculationTemplatesProperty collection (idle).

For now it only does first thing, but not the second, how do I fix it?

Fallingsappy
  • 181
  • 3
  • 21
  • It's not clear what you actually want. You can't _"display current value of TemplateType propertie of items in UserAutoCalculationTemplatesProperty collection"_. How can you _display_ something in a collection? This is why your binding on the `SelectedValue` property don't work - it can't because it absolutely makes no sense. Please try to explain what you want to display and where. You can display something e.g. a selected value in a `TextBlock`. – BionicCode Apr 14 '20 at 11:33
  • I want to bind Combobox to AutoCalculationTemplate's TemplateType property and display it selected value (description of it value) of my observableCollection – Fallingsappy Apr 14 '20 at 13:07
  • 1
    You are actually not binding to any `AutoCalculationTemplate` item or `TemplateType` property, since you are binding the `ComboBox` directly to a collection of the `CalculationVariants` enumeration values, which is returned from your markup extension. You are binding to a collection of `EnumerationMember` items. My answer shows how you can get the selected value from the `ComboBox`. – BionicCode Apr 14 '20 at 14:11

1 Answers1

0

The following example binds the ComboBox.SelectedValue to a property of your OptionsWindow:

public static readonly DependencyProperty SelectedCalculationVariantsProperty = DependencyProperty.Register(
  "SelectedCalculationVariants", 
  typeof(CalculationVariants), 
  typeof(Options));

public CalculationVariants SelectedCalculationVariants
{
    get => (CalculationVariants) GetValue(SelectedCalculationVariantsProperty);
    set => SetValue(SelectedCalculationVariantsProperty, value);
}

<DataGrid.Columns>
  <DataGridTemplateColumn Header="">
    <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
        <ComboBox x:Name="AutoCalculationTypeCombo"
                  ItemsSource="{Binding Source={enum:Enumeration {x:Type options:CalculationVariants}}}"
                  DisplayMemberPath="Description"
                  SelectedValue="{Binding SelectedCalculationVariants, ElementName=OptionsWindow, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                  SelectedValuePath="Value"
                  DropDownClosed="AutoCalculationTypeCombo_OnDropDownClosed"/>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGrid.Columns>
BionicCode
  • 1
  • 4
  • 28
  • 44