C#WPF从数据库自动创建标签

本文关键字:创建 标签 数据库 C#WPF | 更新日期: 2023-09-27 18:23:46

我想从数据库中自动创建标签。对于前任。我有一个带有列[id][name][sname]等的表[worker]。当我创建新的worker时,我希望应用程序用他的名字/sname等创建新的标签。

我试着手动绑定标签,但这不是重点。

              label1.Text = dt.Rows[0]["Worker_Name"].ToString()

是的,我知道它不是从wpf。

在最后一步,我想拖动&将应用程序拖放到我要拖动的位置&将员工(标签)放到新的部门、新的学习者等,但这将在未来发生:)(对不起我的英语)

C#WPF从数据库自动创建标签

编辑

WPF解决方案将被区别对待。我就是这样做的。DataGrid的ItemSource将绑定到ObservableCollection,并在代码后面设置DataContext。

因此,如果你想为网格中的所有条目创建标签,你可以迭代ObservableCollection,实例化一个新的标签,并从ObservableSet中的数据中设置属性。如果您想在用户单击DataGrid中的条目时创建标签,我会执行以下操作(根据其他代码修改!)。

XAML

                        <DataGridTextColumn Binding="{Binding BackR, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="R" Width="40"/>
                        <DataGridTextColumn Binding="{Binding BackG, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="G" Width="40"/>
                        <DataGridTextColumn Binding="{Binding BackB, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="B" Width="40"/>
                        <DataGridTextColumn Binding="{Binding Tags, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="Tags" Width="90"/>
                    </DataGrid.Columns>
                </DataGrid>

在模型中

private ObservableCollection<PaletteEntry> _paletteEntries = new  ObservableCollection<PaletteEntry>();
public ObservableCollection<PaletteEntry> PaletteEntries
{
    get { return _paletteEntries; }
    set { _paletteEntries = value; OnPropertyChanged("PaletteEntries"); }
}
public class PaletteEntry : INotifyPropertyChanged
    {
        private string _count;
        public string Count
        {
            get { return _count; }
            set
            {
                _count = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Count"));
            }
        }
        private string _readOnly;
        public string ReadOnly
        {
            get { return _readOnly; }
            set
            {
                _readOnly = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ReadOnly"));
            }
        }
        private string _displayPalletteType;
        public string DisplayPalletteType
        {
            get { return _displayPalletteType; }
            set
            {
                _displayPalletteType = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DisplayPalletteType"));
            }
        }
        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Title"));
            }
        }
        private SolidColorBrush _background;
        public SolidColorBrush Background
        {
            get { return _background; }
            set
            {
                _background = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Background"));
            }
        }
        private string _backname;
        public string BackName
        {
            get { return _backname; }
            set
            {
                _backname = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BackName"));
            }
        }
        private string _backR;
        public string BackR
        {
            get { return _backR; }
            set
            {
                _backR = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BackR"));
            }
        }
        private string _backG;
        public string BackG
        {
            get { return _backG; }
            set
            {
                _backG = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BackG"));
            }
        }
        private string _backB;
        public string BackB
        {
            get { return _backB; }
            set
            {
                _backB = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BackB"));
            }
        }
        private SolidColorBrush _foreground;
        public SolidColorBrush Foreground
        {
            get { return _foreground; }
            set
            {
                _foreground = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Foreground"));
            }
        }
        private string _forename;
        public string ForeName
        {
            get { return _forename; }
            set
            {
                _forename = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ForeName"));
            }
        }
        private string _foreR;
        public string ForeR
        {
            get { return _foreR; }
            set
            {
                _foreR = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ForeR"));
            }
        }
        private string _foreG;
        public string ForeG
        {
            get { return _foreG; }
            set
            {
                _foreG = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ForeG"));
            }
        }
        private string _foreB;
        public string ForeB
        {
            get { return _foreB; }
            set
            {
                _foreB = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ForeB"));
            }
        }
        private string _tags;
        public string Tags
        {
            get { return _tags; }
            set
            {
                _tags = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Tags"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    };

背后的代码

WindowsColorPallete.DataContext = null;
WindowsColorPallete.DataContext = viewModel.PaletteEntries;
private void WindowsColorPallete_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!WindowsColorPallete.IsReadOnly)
            return;
        DependencyObject dep = (DependencyObject)e.OriginalSource;
        while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
        if (dep == null)
            return;
        if (dep is DataGridCell)
        {
            DataGridCell cell = dep as DataGridCell;
            while ((dep != null) && !(dep is DataGridRow))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            DataGridRow row = dep as DataGridRow;
            var ple = (ColorPickerViewModel.PaletteEntry)row.Item;
            currentPaletteEntry = ple;
            // HERE AS AN EXAMPLE IS WHERE I WOULD INSTANTIATE A NEW LABEL AND SET THE PROPERTIES
            // FROM THE ObservableCollection 
            // EG
            var l = new Label();
            l.Content = ple.Title;
            // ETC :) 
            if (ple.Title != "")
                TitleValue.Text = ple.Title;
            if (ple.Tags != "")
                TagsValue.Text = ple.Tags;
        }
    }

我想我理解你想要实现的目标。这是我很久以前写的一些旧的C#代码。我加载了一个带有数据的数据网格视图,然后对其进行迭代,从而根据需要创建标签。正如您所看到的,它将新标签和新的RichTextBox添加到容器、面板中,并根据数据网格中的信息设置属性数据。它还管理定位。

希望我已经明白你想要什么,这会有所帮助
Jim

        for (int i = 0; i < dgv_Fields.Rows.Count; i++)
        {
            // Add a key field column that has NOT been selected to a column
            if (Convert.ToBoolean(dgv_Fields.Rows[i].Cells[0].Value) ||
                (Convert.ToInt32(dgv_Fields.Rows[i].Cells["Key#"].Value) > 0 &&
                !Convert.ToBoolean(dgv_Fields.Rows[i].Cells[0].Value)))
            {
                dgv_columns.ColumnCount = count + 1;
                cName = FirstLetterToUpper(dgv_Fields.Rows[i].Cells[1].Value.ToString());
                dgv_columns.Columns[count].Name = dgv_Fields.Rows[i].Cells[1].Value.ToString();
                if (Convert.ToInt32(dgv_Fields.Rows[i].Cells["Key#"].Value) > 0)
                    dgv_columns.Columns[count].Tag = dgv_Fields.Rows[i].Cells["Key#"].Value;
                else
                    dgv_columns.Columns[count].Tag = "";
                if ((Convert.ToInt32(dgv_Fields.Rows[i].Cells["Key#"].Value) > 0 &&
                !Convert.ToBoolean(dgv_Fields.Rows[i].Cells[0].Value)))
                    dgv_columns.Columns[count].Name = "*" + dgv_Fields.Rows[i].Cells[1].Value.ToString();
                tbx = x + 160;
                label = new Label();
                label.Name = "l" + count.ToString();
                label.Text = cName.PadRight(25);
                if ((Convert.ToInt32(dgv_Fields.Rows[i].Cells["Key#"].Value) > 0 &&
                !Convert.ToBoolean(dgv_Fields.Rows[i].Cells[0].Value)))
                    label.Text = "*" + cName.PadRight(25);
                label.Location = new Point(x, y);
                label.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
                label.AutoSize = true;
                panel1.Controls.Add(label);
                richtextbox = new RichTextBox();
                richtextbox.Name = "rtb" + count.ToString();
                richtextbox.Location = new Point(tbx + 10, y - 4);
                richtextbox.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
                richtextbox.Size = new Size(100, 35);
                richtextbox.Tag = count.ToString();
                richtextbox.Click += new EventHandler(richtextbox_Click);
                richtextbox.TextChanged += new EventHandler(richtextbox_TextChanged);
                panel1.Controls.Add(richtextbox);
                y += 40;
                count++;
            }
        }