【问题标题】:How can I bind to a property from the Selected Item of a ComboBox?如何从 ComboBox 的 Selected Item 绑定到属性?
【发布时间】:2016-05-14 21:02:02
【问题描述】:

我正在尝试创建一个用户控件,以允许用户在应用程序中定义自己的自定义线性渐变。

到目前为止,我有一个用户控件,它使用以下模型来定义渐变停止的集合:

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
}

我将它用作 ComboBox 的 DataContext(据我所知,ItemSource = Stops,SelectedItem = SelectedStop,双向绑定,标准操作过程)。

我还有一个控件,它使用以下颜色模型来定义颜色(基本上是 4 个滑动条):

/// <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; }
}

所以我们在这里...如何将我的 ComboBox 的 SelectedItem 的 Color 属性与此 ColorModel 联系在一起? (如有必要,将提供更多细节,但时间有点短)。

提前感谢您的帮助...

编辑 1

为了清楚起见 - 我想将 SelectedValue ( Color ) 传递给能够编辑该颜色的控件。

所以操作的顺序是让 ComboBox 选择一个渐变色标,然后(这是我需要帮助的地方)负责调整颜色值的控件。

【问题讨论】:

  • 你为什么要在 OnPopertyChanged 上进行所有这些异步调用??
  • @Steve 虽然与我的问题或问题无关,但它们没有特别的原因。这只是我的偏好。

标签: c# wpf xaml data-binding combobox


【解决方案1】:

如果我理解正确,您希望在选择项目时显示所选的渐变颜色。

我会为它创建自己的 DataTemplate,并将颜色设置为 GradientStop 中运行时构建颜色的绑定。取决于您是否希望所有组合框项目都着色,这可能会有所帮助:Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?

编辑

响应编辑:如果我理解正确,您有 UserControl,DataContext 是一些 UserControlModel,其中的孩子是 StopSelectorModel,在这种情况下,我会设置 SelectedGradientStop 上的引发事件并在 UserControlModel 中处理此事件.

或者,如果不是子父关系,我会使用 Messanger 并在设置 SelectedGradientStop 时发送消息。

事件和消息都必须包含描述应该使用什么颜色的对象。

【讨论】:

  • 没有。那是不对的。我希望能够将颜色值传递给将用于编辑颜色属性的控件。我会在我的问题中澄清......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2016-09-25
  • 2021-02-15
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多