簡體   English   中英

如何從MainView初始化UserControl的ComboBox

[英]How to initialize UserControl's ComboBox from the MainView

我有一個用WPF編寫的UserControl,它實現了“ System.IO.Ports”中的“ SerialPort”類,它包含一些Buttons和一個ComboBox。

它在打開端口,關閉端口並從PC上獲取所有端口的功能背后都有一些代碼。

這是我的XAML的一部分:

<Grid>
    <StackPanel Orientation="Horizontal"
                HorizontalAlignment="Center">
        <TextBlock Text="Choose COM:"
                   VerticalAlignment="Center"
                   Margin="10,0"/>
        <ComboBox Name="ComboBoxPorts"
                  Height="25"
                  Width="80" />
    </StackPanel>
</Grid>

同樣在此XAML背后的代碼中,我具有有效的功能,該功能可從PC獲得所有端口,並且應在ComboBox中填充可用的COM端口。

編輯:

public partial class Communication : UserControl
{
    public SerialPort comPort = new SerialPort();

    public ObservableCollection<string> Ports
    {
        get
        {
            return (ObservableCollection<String>)GetValue(OCProperty);
        }
        set
        {
            SetValue(OCProperty, value);
        }
    }

    public Communication()
    {
        InitializeComponent(); 
    }

    public void GetPortNames()
    {
        int num;
        this.ports = new ObservableCollection<string>();
        string[] port_Names = SerialPort.GetPortNames().OrderBy(a => a.Length > 3 && int.TryParse(a.Substring(3), out num) ? num : 0).ToArray();

        foreach (string port in port_Names)
            this.ports.Add(port);
        this.DataContext = this;
        ComboBoxPorts.ItemsSource = this.ports;
        ComboBoxPorts.SelectedItem = this.ports.FirstOrDefault(); // object to show on comboBox
    }


    public static readonly DependencyProperty OCProperty =
    DependencyProperty.Register("Ports", typeof(ObservableCollection<String>), typeof(Communication));

在我的MainWindow中,我調用該UserControl元素,然后在加載窗口之后,ComboBox仍然為空,其中沒有任何字符串。 如何使我可以在用戶控件中定義的ComboBox中看到COM端口?

主窗口代碼:

編輯:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

我的MainWindow XAML:

編輯:

<Window x:Class="MyLabratory.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyProject ="clr-namespace:MyLabratory"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <MyProject:Communication />
</Grid>

謝謝你的幫助!

您必須將用戶控件添加到可視化樹中,您只是實例化了它,但隨后還需要添加到布局控件,例如Grid

而不是通過編程方式創建控件:

Communication communicator = new Communication();

我會在Xaml中創建它:

<Communication Name="communicator"/>

首先,您應該為您的項目使用MVVM模式,而不是應該從視圖模型中完成獲取端口名和填充ComboBox類的所有邏輯。

對於您的情況,請使用以下代碼:

public partial class Communication : UserControl
{
    public SerialPort comPort = new SerialPort();
    public ObservableCollection<string> Ports {get;set;}

    public Communication()
    {
        InitializeComponent(); 
    }

    public void GetPortNames()
    {
        int num;
        this.Ports= new ObservableCollection<string>();
        string[] port_Names = SerialPort.GetPortNames().OrderBy(a => a.Length > 3 && int.TryParse(a.Substring(3), out num) ? num : 0).ToArray();

        foreach (string port in port_Names)
            this.Ports.Add(port);
        this.DataContext=this;
        ComboBoxPorts.ItemsSource = this.Ports;
        ComboBoxPorts.SelectedItem = this.Ports.FirstOrDefault(); // object to show on comboBox
    }
}

創建類型為ObservableCollection<string>的屬性,該屬性將用作ComboBox項源。 將項目添加到ObservableCollection或從ObservableCollection移除時,UI會更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM