[英]Create UserControl ComboBox with Button
I'm trying to create an UserControl that have a ComboBox with a list of items and then I want to add a button to perform some action. 我正在尝试创建一个具有ComboBox的UserControl,其中ComboBox带有项目列表,然后我想添加一个按钮来执行某些操作。
I have this code: 我有以下代码:
ComboBoxWithButton XAML: ComboBoxWithButton XAML:
<ComboBox x:Name="ComboBoxBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,0,-1" Width="100" ItemsSource="{Binding Source}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Content="{Binding }" Width="100" />
<Button Grid.Column="1">Something</Button>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
ComboBoxWithButton Code: ComboBoxWithButton代码:
public ComboBoxWithButton()
{
InitializeComponent();
}
public static readonly DependencyProperty SourceProprety =
DependencyProperty.Register("Source", typeof(ObservableCollection<object>), typeof(ComboBoxWithButton), new PropertyMetadata(null));
public ObservableCollection<object> Source
{
get { return (ObservableCollection<object>)GetValue(ValueProprety); }
set { SetValue(ValueProprety, value); }
}
Then on my Window I call: 然后在我的窗口上调用:
<controls:ComboBoxWithButton Source="{Binding SomeCollection}"/>
But the combobox is empty 但是组合框是空的
Make three changes in your dependency property: Change its type to System.Collections.IEnumerable
(non-generic), correct the name of SourceProprety
to SourceProperty
, and use SourceProperty
rather than ValueProperty
in your calls to GetValue()
and SetValue()
(that last bug won't affect the ComboBox
, because a Binding
will call SetValue()
and GetValue()
directly, but it's still a bug): 在依赖项属性中进行三处更改:将其类型更改为
System.Collections.IEnumerable
(非通用),将SourceProprety
的名称更正为SourceProperty
,并在对GetValue()
和SetValue()
调用中使用SourceProperty
而不是ValueProperty
。最后一个错误不会影响ComboBox
,因为Binding
将直接调用SetValue()
和GetValue()
,但这仍然是一个错误):
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source",
typeof(IEnumerable),
typeof(ComboBoxWithButton),
new PropertyMetadata(null));
public IEnumerable Source
{
get { return (IEnumerable)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
Your definition of the property required the collection to be of type ObservableCollection<object>
. 您对属性的定义要求集合的类型为
ObservableCollection<object>
。 Only object
, nothing but object
. 只有
object
,除了object
。 If you bound ObservableCollection<MyItem>
to it, it would fail to populate the ComboBox
. 如果将
ObservableCollection<MyItem>
绑定到它,它将无法填充ComboBox
。 ComboBox.ItemsSource
is of type System.Collections.IEnumerable
(the property is inherited from ItemsControl
). ComboBox.ItemsSource
的类型为System.Collections.IEnumerable
(该属性继承自ItemsControl
)。 Stick with that. 坚持下去。 It works.
有用。 Any
ObservableCollection
of anything can be bound to that. 任何东西的任何
ObservableCollection
都可以绑定到该对象。 Let the ComboBox
figure out the details; 让
ComboBox
找出细节; that's what it does best. 那才是最好的。
Next, your binding is looking at the UserControl
's DataContext
for the Source
property, but it's a property of the UserControl
itself. 接下来,您的绑定正在查看
Source
属性的UserControl
的DataContext
,但这是UserControl
本身的属性。 Here's how to fix that: 解决方法如下:
<ComboBox
x:Name="ComboBoxBtn"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="0,0,0,-1"
Width="100"
ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"
>
The XAML designer may give you a blue squiggle under the ItemsSource
line. XAML设计器可能会在
ItemsSource
行下给您一个蓝色的ItemsSource
。 Let it moan, it just gets confused sometimes. 让它抱怨,有时会感到困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.