亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將按鈕和組合框放在一起

如何將按鈕和組合框放在一起

C#
12345678_0001 2023-09-16 17:45:08
我想要一個帶有按鈕的組合框,如下所示:因為我想使用它,以便可以選擇項目并將其添加到 ListView。問題:我不知道如何獲取按鈕中的圖標,如圖所示如何讓它們很好地排列在一起,或者有沒有辦法將我不知道的兩個元素結合起來?
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

這是一個工作示例。


假設您的用戶控件有兩個控件;一個ComboBox和一個Button。您希望能夠將主控件(父控件)中的某些內容綁定到用戶控件。然后,在選擇某些內容并單擊按鈕后,您希望用戶控件將事件發生通知給父級,并傳遞所選值。


用戶控件XAML:


<UserControl ...

             d:DesignHeight="40" d:DesignWidth="200">

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="160"/>

            <ColumnDefinition Width="40"/>

        </Grid.ColumnDefinitions>


        <ComboBox Grid.Column="0" Margin="4" Name="ItemsComboBox"

                  ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"/>

        <Button Grid.Column="1" Margin="4" Content="+"

                Click="Button_Click"/>

    </Grid>

</UserControl>

以下綁定將允許您將數據列表綁定到組合框(形成父級):


ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"

在您的 中MainWindow,您將像這樣使用該控件:


<Grid>

    <local:UCComboButton Grid.Row="0" Width="200" Height="40" x:Name="MyUC"

                         Source="{Binding Names}"/>

</Grid>

并在UserControl后面的s代碼中:


public partial class UCComboButton : UserControl

{

    public UCComboButton()

    {

        InitializeComponent();

    }


    // We use this dependency property to bind a list to the combo box.

    public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(IEnumerable), typeof(UCComboButton), new PropertyMetadata(null));


    public IEnumerable Source

    {

        get { return (IEnumerable)GetValue(SourceProperty); }

        set { SetValue(SourceProperty, value); }

    }


    // This is to send the occurred event, in this case button click, to the parent, along with the selected data.

    public class SelectedItemEventArgs : EventArgs

    {

        public string SelectedChoice { get; set; }

    }


    public event EventHandler<SelectedItemEventArgs> ItemHasBeenSelected;


    private void Button_Click(object sender, RoutedEventArgs e)

    {

        var selected = ItemsComboBox.SelectedValue;

        ItemHasBeenSelected?.Invoke(this, new SelectedItemEventArgs { SelectedChoice = selected.ToString() });

    }

}

現在在MainWindow.xaml.cs:


public MainWindow()

{

    InitializeComponent();

    // Subscribe to the item selected event

    MyUC.ItemHasBeenSelected += UCButtonClicked;


    Names = new List<string>

    {

        "A",

        "B",

        "C"

    };


    DataContext = this;

}


void UCButtonClicked(object sender, UCComboButton.SelectedItemEventArgs e)

{

    var value = e.SelectedChoice;

    // Do something with the value

}

請注意,上面的Names列表是從主窗口綁定到用戶控件的內容XAML。


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 112 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號