简体   繁体   中英

How can I bind to a property from the Selected Item of a ComboBox?

I am attempting to create a user control for the purposes of allowing a user to define their own custom linear gradient within an application.

So far I have a user control which makes use of the following model for defining a collection of Gradient Stops :

public class StopSelectorModel : INotifyPropertyChanged {
    #region Field Values
    private Task _PropertyT;
    private ObservableCollection<GradientStop> _Stops;
    private GradientStop _SelectedStop;
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region Properties
    /// <summary>
    /// Get or Set Gradient Stop Collection.
    /// </summary>
    public ObservableCollection<GradientStop> Stops {
        get { return this._Stops; }
        set {
            this._Stops = value;
            this._SelectedStop = value.FirstOrDefault( );

            this._PropertyT = Task.WhenAll( new Task[ ] {
                this.OnPropertyChanged( "Stops" ),
                this.OnPropertyChanged( "SelectedStop" )
            } );
        }
    }

    /// <summary>
    /// Get or Set Selected Gradient Stop.
    /// </summary>
    public GradientStop SelectedStop {
        get { return this._SelectedStop; }
        set {
            if ( value == null )
                return;

            if ( !this._Stops.Contains( value ) )
                this._Stops.Add( value );
            this._SelectedStop = value;

            this._PropertyT = this.OnPropertyChanged( "SelectedStop" );
        }
    }
    #endregion

    #region Methods
    protected async Task OnPropertyChanged( string P ) {
        if ( this.PropertyChanged != null )
            await this.PropertyChanged.Async( this, new PropertyChangedEventArgs( P ) );
    }
    #endregion

    #region Constructors
    /// <summary>
    /// Declare instance of StopSelectorModel.
    /// </summary>
    /// <param name="Base">GradientStopCollection to wrap.</param>
    public StopSelectorModel( ObservableCollection<GradientStop> Base ) { this.Stops = Base; }

    /// <summary>
    /// Declare default instance of StopSelectorModel.
    /// </summary>
    public StopSelectorModel( ) : this( new ObservableCollection<GradientStop>( new GradientStop[ ] {
        new GradientStop( Colors.White, 0.0D ),
        new GradientStop( Colors.Black, 1.0D )
    } ) ) { }
    #endregion
}

I use this as a DataContext for a ComboBox ( ItemSource = Stops, SelectedItem = SelectedStop, two-way binding, standard operating procedure for something like this as far as I am aware ).

I also have a control which uses the following Color Model to define colors ( 4 slide bars basically ) :

/// <summary>
/// Wrapper for allowing complete databinding of a System.Windows.Media.Color struct.
/// </summary>
public class ColorModel : INotifyPropertyChanged {
    private Task _PropertyT;
    private Color _Color;

    /// <summary>
    /// Get or Set Context Color.
    /// </summary>
    public Color Color {
        get { return this._Color; }
        set {
            this._Color = value;
            this._PropertyT = Task.WhenAll( new Task[ ] {
                this.OnPropertyChanged( "Color" ),
                this.OnPropertyChanged( "ScA" ),
                this.OnPropertyChanged( "ScR" ),
                this.OnPropertyChanged( "ScG" ),
                this.OnPropertyChanged( "ScB" ),
            } );
        }
    }

    /// <summary>
    /// Get or Set Color ScA value.
    /// </summary>
    public double ScA {
        get { return this._Color.ScA; }
        set {
            this._Color.ScA = ( float )value;
            this._PropertyT = Task.WhenAll( new Task[ ] {
                this.OnPropertyChanged( "Color" ),
                this.OnPropertyChanged( "ScA" ),
            } );
        }
    }

    /// <summary>
    /// Get or Set Color ScR value.
    /// </summary>
    public double ScR {
        get { return this._Color.ScR; }
        set {
            this._Color.ScR = ( float )value;
            this._PropertyT = Task.WhenAll( new Task[ ] {
                this.OnPropertyChanged( "Color" ),
                this.OnPropertyChanged( "ScR" ),
            } );
        }
    }

    /// <summary>
    /// Get or Set Color ScG value.
    /// </summary>
    public double ScG {
        get { return this._Color.ScG; }
        set {
            this._Color.ScG = ( float )value;
            this._PropertyT = Task.WhenAll( new Task[ ] {
                this.OnPropertyChanged( "Color" ),
                this.OnPropertyChanged( "ScG" ),
            } );
        }
    }

    /// <summary>
    /// Get or Set Color ScB value.
    /// </summary>
    public double ScB {
        get { return this._Color.ScB; }
        set {
            this._Color.ScB = ( float )value;
            this._PropertyT = Task.WhenAll( new Task[ ] {
                this.OnPropertyChanged( "Color" ),
                this.OnPropertyChanged( "ScB" ),
            } );
        }
    }

    protected async Task OnPropertyChanged( string P ) {
        await this.PropertyChanged?.Async(
            this, new PropertyChangedEventArgs( P ) ).DontBlock( );
    }
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Define ColorModel with default White Color.
    /// </summary>
    public ColorModel( ) : this( Colors.White ) { }

    /// <summary>
    /// Define ColorModel with provided color.
    /// </summary>
    /// <param name="C">Color to assign to ColorModel.</param>
    public ColorModel( Color C ) { this.Color = C; }
}

So here we are... how can I tie the Color property of the SelectedItem of my ComboBox together with this ColorModel? ( Will furnish more details if/as necessary but sort of short on time ).

Thank you for your help in advance...

EDIT 1

For a bit of clarity - I want to pass the SelectedValue ( Color ) to a control which will be able to edit that color.

So the order of operations would be to have the ComboBox select one of the Gradient Stops, and then ( and this is where I need help ) the control which is responsible for adjusting the color values.

If I understand you right you want to get chosen gradient color shown when item is chosen.

I would go with creating own DataTemplate for it with setting color as a binding for run time build color in GradientStop. Depending if you want all your combobox items colored or not this might be helpful: Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?

In response to edit: if I understand it right you've got UserControl with DataContext being some UserControlModel, which on of childrens is StopSelectorModel, in that case I would go with raising event on SelectedGradientStop is set and handling this event in UserControlModel.

Or, if it's not children-parent relation I would use Messanger and send message whenever SelectedGradientStop is set.

Both event and message would have to contain object describing what color should be used.

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