如何将按钮和组合框放在一起

我想要一个带有按钮的组合框,如下所示:

https://img3.mukewang.com/6505795900013acd06210114.jpg

因为我想使用它,以便可以选择项目并将其添加到 ListView。

问题:

  1. 我不知道如何获取按钮中的图标,如图所示

  2. 如何让它们很好地排列在一起,或者有没有办法将我不知道的两个元素结合起来?


12345678_0001
浏览 50回答 1
1回答

泛舟湖上清波郎朗

这是一个工作示例。假设您的用户控件有两个控件;一个ComboBox和一个Button。您希望能够将主控件(父控件)中的某些内容绑定到用户控件。然后,在选择某些内容并单击按钮后,您希望用户控件将事件发生通知给父级,并传递所选值。用户控件XAML:<UserControl ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d:DesignHeight="40" d:DesignWidth="200">&nbsp; &nbsp; <Grid>&nbsp; &nbsp; &nbsp; &nbsp; <Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="160"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="40"/>&nbsp; &nbsp; &nbsp; &nbsp; </Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; <ComboBox Grid.Column="0" Margin="4" Name="ItemsComboBox"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"/>&nbsp; &nbsp; &nbsp; &nbsp; <Button Grid.Column="1" Margin="4" Content="+"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Click="Button_Click"/>&nbsp; &nbsp; </Grid></UserControl>以下绑定将允许您将数据列表绑定到组合框(形成父级):ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"在您的 中MainWindow,您将像这样使用该控件:<Grid>&nbsp; &nbsp; <local:UCComboButton Grid.Row="0" Width="200" Height="40" x:Name="MyUC"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Source="{Binding Names}"/></Grid>并在UserControl后面的s代码中:public partial class UCComboButton : UserControl{&nbsp; &nbsp; public UCComboButton()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; // We use this dependency property to bind a list to the combo box.&nbsp; &nbsp; public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(IEnumerable), typeof(UCComboButton), new PropertyMetadata(null));&nbsp; &nbsp; public IEnumerable Source&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return (IEnumerable)GetValue(SourceProperty); }&nbsp; &nbsp; &nbsp; &nbsp; set { SetValue(SourceProperty, value); }&nbsp; &nbsp; }&nbsp; &nbsp; // This is to send the occurred event, in this case button click, to the parent, along with the selected data.&nbsp; &nbsp; public class SelectedItemEventArgs : EventArgs&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string SelectedChoice { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public event EventHandler<SelectedItemEventArgs> ItemHasBeenSelected;&nbsp; &nbsp; private void Button_Click(object sender, RoutedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var selected = ItemsComboBox.SelectedValue;&nbsp; &nbsp; &nbsp; &nbsp; ItemHasBeenSelected?.Invoke(this, new SelectedItemEventArgs { SelectedChoice = selected.ToString() });&nbsp; &nbsp; }}现在在MainWindow.xaml.cs:public MainWindow(){&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; // Subscribe to the item selected event&nbsp; &nbsp; MyUC.ItemHasBeenSelected += UCButtonClicked;&nbsp; &nbsp; Names = new List<string>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "A",&nbsp; &nbsp; &nbsp; &nbsp; "B",&nbsp; &nbsp; &nbsp; &nbsp; "C"&nbsp; &nbsp; };&nbsp; &nbsp; DataContext = this;}void UCButtonClicked(object sender, UCComboButton.SelectedItemEventArgs e){&nbsp; &nbsp; var value = e.SelectedChoice;&nbsp; &nbsp; // Do something with the value}请注意,上面的Names列表是从主窗口绑定到用户控件的内容XAML。
打开App,查看更多内容
随时随地看视频慕课网APP