wpf 自定义控件combobox 依赖属性
我新建了一个usercontrol,里面套了一个ComboBox代码如下<UserControl><ComboBoxItemsSource="{BindingSource...
我新建了一个usercontrol,里面套了一个ComboBox
代码如下
<UserControl >
<ComboBox ItemsSource="{Binding Source={x:Static local:CustomCombobox.SubjectList}}"
SelectedItem="{Binding Path=SelectedSubject,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Code"/>
</UserControl>
自定义控件的后台代码
public partial class CustomCombobox : UserControl
{
public CustomCombobox()
{
InitializeComponent();
}
static CustomCombobox()
{
CustomCombobox.SubjectList = new List<Subject>()
{
new Subject("1001","cash"),
new Subject("1002","bank"),
new Subject("5505","revenue")
};
SelectedSubjectProperty = DependencyProperty.Register(
"SelectedSubject", typeof(Subject), typeof(CustomCombobox)
);
}
public static List<Subject> SubjectList;
public Subject SelectedSubject
{
get { return (Subject)GetValue(SelectedSubjectProperty); }
set { SetValue(SelectedSubjectProperty,value); }
}
public static DependencyProperty SelectedSubjectProperty;
}
辅助类:
public class Subject
{
public Subject(string s, string t)
{
this.Code = s;
this.Name = t;
}
public string Code { get; set; }
public string Name { get; set; }
}
在这个自定义控件有一个依赖项属性SelectedSubject,是为了绑定嵌套的Combobox的SelectdItem。
然后,我在其他窗口使用这个控件
<Window>
<Grid>
<local:CustomCombobox Width="100" Height="30" x:Name="customcbb" SelectedSubject="{Binding Path=SelectSubject}"/>
</Grid>
</Window>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public Subject SelectSubject
{
get;
set;
}
}
MainWindow 的SelectSubject属性绑定自定义控件的依赖项属性SelectedSubject,但是并没有成功关联,问题到底出在哪里呀
已解决,binding mode=twoway 就行了 ,汗。。。。 展开
代码如下
<UserControl >
<ComboBox ItemsSource="{Binding Source={x:Static local:CustomCombobox.SubjectList}}"
SelectedItem="{Binding Path=SelectedSubject,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Code"/>
</UserControl>
自定义控件的后台代码
public partial class CustomCombobox : UserControl
{
public CustomCombobox()
{
InitializeComponent();
}
static CustomCombobox()
{
CustomCombobox.SubjectList = new List<Subject>()
{
new Subject("1001","cash"),
new Subject("1002","bank"),
new Subject("5505","revenue")
};
SelectedSubjectProperty = DependencyProperty.Register(
"SelectedSubject", typeof(Subject), typeof(CustomCombobox)
);
}
public static List<Subject> SubjectList;
public Subject SelectedSubject
{
get { return (Subject)GetValue(SelectedSubjectProperty); }
set { SetValue(SelectedSubjectProperty,value); }
}
public static DependencyProperty SelectedSubjectProperty;
}
辅助类:
public class Subject
{
public Subject(string s, string t)
{
this.Code = s;
this.Name = t;
}
public string Code { get; set; }
public string Name { get; set; }
}
在这个自定义控件有一个依赖项属性SelectedSubject,是为了绑定嵌套的Combobox的SelectdItem。
然后,我在其他窗口使用这个控件
<Window>
<Grid>
<local:CustomCombobox Width="100" Height="30" x:Name="customcbb" SelectedSubject="{Binding Path=SelectSubject}"/>
</Grid>
</Window>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public Subject SelectSubject
{
get;
set;
}
}
MainWindow 的SelectSubject属性绑定自定义控件的依赖项属性SelectedSubject,但是并没有成功关联,问题到底出在哪里呀
已解决,binding mode=twoway 就行了 ,汗。。。。 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询