阅读背景:

WPF data binding a List of strings to a combobox

来源:互联网 
<UserControl x:Class="blah..my_UserControl "
                 xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="https://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:blah."
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <GroupBox x:Name="groupBox" Header="Settings" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Top" RenderTransformOrigin="-0.176,-0.343" Width="503" Height="85">
            <Grid>

                <ComboBox x:Name="comboBox" ItemsSource="{Binding PortList}" HorizontalAlignment="Left" Margin="0,30,0,0" VerticalAlignment="Top" Width="120" >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBox Text="{Binding}" Width="16" Height="16" Margin="0,2,5,2" />
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                </Grid>
            </GroupBox>
        </Grid>
</UserControl>

public partial class Child: Base
{
    public Child()
    {
        PortList.add("com1");
    }
}

public abstract class Base : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
    private ObservableCollection<string> portlist;
    public ObservableCollection<string> PortList
    {
        get { return portlist; }
        set
        {
            portlist = value;
            NotifyPropertyChanged("PortList");
        }
    }
    public Base()
    {
        PortList = new ObservableCollection<string>();
    }
}

public partial class my_UserControl : UserControl
{
    public my_UserControl(Base base)
    {
        InitializeComponent();
        DataContext = base;
    }
}
<UserControl x:Class="blah..my_UserControl "
  



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: