gpt4 book ai didi

c# - 如何绑定(bind)到 ComboBox 的 Selected Item 中的属性?

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:40 35 4
gpt4 key购买 nike

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

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

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,双向绑定(bind),据我所知,这是类似此类的标准操作程序)。

我还有一个控件,它使用以下颜色模型来定义颜色(基本上是 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 选择一个渐变停止点,然后(这是我需要帮助的地方)负责调整颜色值的控件。

最佳答案

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

我会为它创建自己的 DataTemplate,并将颜色设置为 GradientStop 中运行时构建颜色的绑定(bind)。根据您是否希望所有组合框项目都着色,这可能会有所帮助: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 时发送消息。

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

关于c# - 如何绑定(bind)到 ComboBox 的 Selected Item 中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231843/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com