假如我現在創建一個用戶控制元件叫做:DualTextBox
里面包含一個ComboBox 和一個textbox
<UserControl x:Class="WPF.Extend.Controls.DualTextBox">
<Grid Height="25" Margin="1" Background="#C4F0D9">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<ComboBox Name="CbbRegS" Height="25" MinWidth="80"
MinHeight="25" IsEditable="True" />
<TextBox Width ="50" Grid.Column = "1"/>
</Grid>
</UserControl>
現在我別的專案使用這個控制元件,
請問如何將我的一個list或者陣列系結到這個ComboBox, 系結的時候希望能夠使用DualTextBox.CmbItemSource (而不是ItemSource)
同時也將我的一個變數系結到這個TextBox,系結的時候希望能夠使用DualTextBox.TbText(而不是Text)
uj5u.com熱心網友回復:
定義對應的依賴項屬性,查詢 WPF 依賴項屬性即可。uj5u.com熱心網友回復:
你這樣的需求不應該用UserControl解決,自定義一個控制元件,繼承自ComboBox,這樣集合屬性你不用自己寫了,然后需要為TextBox的Text頂一個依賴項屬性,最后在你自定義的控制元件的控制元件模板上設計你的要怎么擺放這兩個控制元件就可以了uj5u.com熱心網友回復:
自定義依賴屬性,類似于:public static readonly DependencyProperty GradeListProperty = DependencyProperty.Register("GradeList", typeof(IList<Grade>), typeof(CourseHourDataGrid), new FrameworkPropertyMetadata(null));
public IList<Grade> GradeList
{
get
{
return (IList<Grade>)base.GetValue(GradeListProperty);
}
set
{
base.SetValue(GradeListProperty, value);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/116542.html
