0

Problem

I have a UserControl which contains a ToggleButton and a ComboBox. The control will allow the user to choose a sort type (via ComboBox) and a Direction (via ToggleButton). I want to be able to expose some properties of the ComboBox and more, so how do I bind the ItemsSource of the ComboBox to an Items Property of the UserControl, which I will implement myself, but also the built-in Content property---similar to how a ComboBox can do both.

UserControl

I have a user control which set-up is similar as the below code, or look here.

<UserControl x:Class="Example.DirComboBox">
    <Grid>
        <ComboBox x:Name="cbItems" />
        <ToggleButton x:Name="tbSortDir"/>
    </Grid>
</UserControl>

Control Usage

I would like to be able to use it in two ways:

1:

Adding Child Elements.

<local:DirComboBox>
    <ComboboxItem Content="Item 1"/>
</local:DirComboBox>

2:

Binding Items Property.

<local:DirComboBox Items="{Binding SortList}"/>

Alternatives

I would be willing to use alternatives, such as setting the root as ComboBox instead of UserControl but I need to expose the follow (but not sure how):

  1. Have a ToggleButton on the side,
  2. SortDirection property as a bool
  3. RoutedEvent for Ascending and Descending
4

2 回答 2

0

在您SortDirection的. 在控件中拥有这些属性后,您可以直接从外部设置它们,例如:Itemsusercontrol

<local:DirComboBox Items="{Binding SortList}" SortDirection="{Binding Sort}"/>

然后在您的控件内将这些属性绑定到相应的控件,例如:

<UserControl x:Class="Example.DirComboBox">
    <Grid>
        <ComboBox x:Name="cbItems" ItemsSource="{Binding Items, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}"}/>
        <ToggleButton x:Name="tbSortDir" IsPressed="{Binding SortDirection, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}"/>
    </Grid>
</UserControl>

保持绑定模式为双向。

于 2013-09-04T17:54:55.953 回答
0

根据MSDN 上的教程,UserControl我改为使用 CustomControl,而不是基于我的控制。

于 2013-09-05T20:31:55.060 回答