简体   繁体   中英

Bind combobox to enum and collection of class that has this enum

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM