首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# WPF从数据库自动创建标签

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

Stack Overflow用户
提问于 2014-09-21 21:21:59
回答 1查看 337关注 0票数 0

我想从数据库自动创建标签。为了前夫。我有列、idsname等列的表工作者。当我创建新的工作者时,我希望应用程序创建带有他的名字/名称等的新标签。

我试着用手工装订标签,但这不是重点。

代码语言:javascript
复制
              label1.Text = dt.Rows[0]["Worker_Name"].ToString()

是的,我知道这不是来自wpf。

在最后一步,我希望拖拽应用程序,我将拖放工人(标签)到新的部分,新的学习人员等,但这将是在未来:)(对不起,我的英语)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-22 00:17:42

编辑

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

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

XAML

代码语言:javascript
复制
                        <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>

在模型中

代码语言:javascript
复制
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));
        }
    };

在“守则”后面

代码语言:javascript
复制
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#代码。我用数据加载了一个datagridview,然后迭代它,从而根据需要创建标签。如您所见,它将新标签和新RichTextBox添加到容器、面板中,并根据数据集中的信息设置属性数据。它还管理了定位。

希望我已经理解了你想要的,这有帮助。

吉姆

代码语言:javascript
复制
        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++;
            }
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25963994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档