在WPF项目中创建简单DataGrid并设置其单元格字体的最简单方法是什么 [英] What is the easiest way to create a simple DataGrid and set its cells font in WPF project

查看:107
本文介绍了在WPF项目中创建简单DataGrid并设置其单元格字体的最简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的WPF项目中(使用C#),我有一个包含DataGrid的XAML。 DataGrid有4列标题(第一个为空)。在加载时,我添加一组行,并在每行的第二个单元格中添加一个字符串。我想,我设法添加它们,但它们似乎没有可见或根本没有添加!我尝试改变文字的颜色但找不到简单的方法。



我是WPF的新手,所以有人可以告诉我最简单的方法是什么添加新行并设置其单元格的文本及其字体颜色??



这是我的xaml:

 <  网格 >  
< 按钮 内容 = 按钮 Horizo​​ntalAlignment = Left 保证金 = 10,36,0,0 VerticalAlignment = Top 宽度 = 75 点击 = Button_Click / >
< DataGrid x:名称 = DGV Horizo​​ntalAlignment = 保证金 = 10,60,-88,0 VerticalAlignment = Top 宽度 = 554 高度 = 103 Panel.ZIndex = 1 < span class =code-attribute> Rowremoved = #FFC6C6C6 FontWeight = 粗体 CanUserSortColumns = False CanUserResizeColumns = False CanUserReorderColumns = False MinColumnWidth = 10 IsReadOnly = >
< DataGrid.Columns >
< DataGridTextColumn 标题 = / >
< DataGridTextColumn 标题 = 网址 < span class =code-attribute> 宽度 = 295 / >
< 数据GridTextColumn 标题 = Ahrefs(http) 宽度 = 79 / >
< DataGridTextColumn 标题 = Ahrefs(www。) 宽度 = 79 / >
< 数据GridTextColumn 标题 = 存档 宽度 = 79 / >
< / DataGrid.Columns >
< / DataGrid >

< / Grid >





这是我的xaml.cs:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Data;
使用 System.Windows.Documents;
使用 System.Windows.Input;
使用 System.Windows.Media;
使用 System.Windows.Media.Imaging;
使用 System.Windows.Navigation;
使用 System.Windows.Shapes;

命名空间 WpfApplication1
{
/// < 摘要 >
/// MainWindow.xaml $ b的交互逻辑$ b /// < < span class =code-summarycomment> / summary >
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}

public struct MyDomain
{
public string id { set ; get ; }
public string url { set ; get ; }
public string AhrefsHttp { set ; get ; }
public string AhrefsWww { set ; get ; }
public string 存档{ set ; get ; }
}


私有 void Button_Click( object sender,RoutedEventArgs e)
{
List< string> LST;

lst = new List< string>();

lst.Add( google.com);
lst.Add( yahoo.com);
lst.Add( facebook.com);

foreach string link in lst)
{
DGV.Items.Add( new MyDomain {id = ,url = link,AhrefsHttp = ,AhrefsWww = ,Archive = });
}
}
}
}

解决方案

这对于完全没有意义将任何样式放在XAML元素的中。如果可以,你怎么可能重复使用它?如果您不需要重复使用样式,为什么要重复使用?



如果您只需要为一个元素执行此操作,则可以直接添加所有这些属性它:

 <   DataGrid     FontSize   =  ...     FondFamily   =  ...    FontWeigh   =  ...     ...    < span class =code-keyword>>  
<! - ... - >
< / DataGrid >





如果您需要与其他元素(可能是不同类型)保持一致,为了保持一致性,您可以创建资源字典并引用所有这本词典的属性一个接一个。这里解释使用资源字典: http://msdn.microsoft.com /en-us/library/cc903952%28v=vs.95%29.aspx [ ^ ]。



您可以使用已定义的资源元素 StaticResource 绑定: http://msdn.microsoft.com/en-us/library/cc903952%28v=vs.95%29.aspx#referencing_resources_from_xaml [ ^ ]。



您可以使用合并资源字典定义与窗口类型XAML分开的资源字典;这样,您可以在多个窗口或用户控件类型中重用原始外部资源字典(由其相对文件名引用): http://msdn.microsoft.com/en-us/library/aa350178%28v=vs.110%29.aspx [ ^ ]。



最后,你可以使用样式重用它们用于不同的元素和不同的窗口或用户控件:

http://msdn.microsoft.com/en-us/library/ms745683%28v=vs.110%29。 aspx [ ^ ],

http://wpftutorial.net/Styles.html [ ^ ]。



-SA


试试这些链接 -

更改行WPF网格样式 [ ^ ]

使用样式和模板的WPF DataGrid自定义 [ ^ ]

WPF DataGrid - 根据标题条件和其他属性设置行和列样式 [ ^ ]


1)我不得不添加绑定资源:

< datagridtextcolumn header =URLwidth =295binding ={Binding url} >

< datagridtextcolumn header =Ahrefs(http)width =79binding ={Binding AhrefsHttp}>

< datagridtextcolumn header = Ahrefs(www。)width =79binding ={Binding AhrefsWww}>

< datagridtextcolumn header =Archivewidth =79binding ={Binding Archive} >



2)对于单元格样式,其他公认解决方案提到的链接很有帮助。



3)以编程方式编辑单元格中的文本:单击此处

Hi,
In my WPF project (using C#) I have an XAML that contains a DataGrid. DataGrid has 4 columns headers (first one empty). On load I add a set of rows and add in the second cell of each row a string. I managed to add them, I guess, but they don't seem to be visible or maybe not added at all! I tried changing the color of text but couldn't find an easy way.

I'm new to WPF, so please can someone tell me what's the easiest way to add new rows and set their cells's text and their font color??

This is my xaml:

<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <DataGrid x:Name="DGV" HorizontalAlignment="Left" Margin="10,60,-88,0" VerticalAlignment="Top" Width="554" Height="103" Panel.ZIndex="1" Rowremoved="#FFC6C6C6" FontWeight="Bold" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" MinColumnWidth="10" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Header=" "/>
            <DataGridTextColumn Header="URL" Width="295"/>
            <DataGridTextColumn Header="Ahrefs(http)" Width="79"/>
            <DataGridTextColumn Header="Ahrefs(www.)" Width="79"/>
            <DataGridTextColumn Header="Archive" Width="79"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>



And this is my xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public struct MyDomain
        {
            public string id { set; get; }
            public string url { set; get; }
            public string AhrefsHttp { set; get; }
            public string AhrefsWww { set; get; }
            public string Archive { set; get; }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<string> lst;

            lst = new List<string>();

            lst.Add("google.com");
            lst.Add("yahoo.com");
            lst.Add("facebook.com");

            foreach (string link in lst)
            {
                DGV.Items.Add(new MyDomain { id = " ", url = link, AhrefsHttp = "", AhrefsWww = "", Archive = "" });
            }
        }
    }
}

解决方案

It makes absolutely no sense to put any style inside of the XAML element. If you could, how could you possibly reuse it? If you don't need a style reuse, why having it at all?

If you need to do it for only one element, you can directly add all those properties to it:

<DataGrid FontSize="...", FondFamily="..." FontWeigh="..." ... >
   <!-- ... -->
</DataGrid>



If you need to be consistent with other elements, possibly of different type, for consistency, you can create a resource dictionary and reference all the property from this dictionary one by one. Using Resource Dictionaries is explained here: http://msdn.microsoft.com/en-us/library/cc903952%28v=vs.95%29.aspx[^].

You can use the defined resource element using the StaticResource binding: http://msdn.microsoft.com/en-us/library/cc903952%28v=vs.95%29.aspx#referencing_resources_from_xaml[^].

You can define a resource dictionary separate from your window-type XAML using the merged resource dictionaries; this way, you can reuse the original external resource dictionary (referenced by its relative file name) in more than one window or user control type: http://msdn.microsoft.com/en-us/library/aa350178%28v=vs.110%29.aspx[^].

Finally, you can use styles are reuse them for different elements and different windows or user controls:
http://msdn.microsoft.com/en-us/library/ms745683%28v=vs.110%29.aspx[^],
http://wpftutorial.net/Styles.html[^].

—SA


Try these links -
Change row style of WPF grid[^]
WPF DataGrid Custommization using Style and Template[^]
WPF DataGrid – Styling rows and columns based on Header conditions and other properties[^]


1) I had to add binding resources:
<datagridtextcolumn header="URL" width="295" binding="{Binding url}">
<datagridtextcolumn header="Ahrefs(http)" width="79" binding="{Binding AhrefsHttp}">
<datagridtextcolumn header="Ahrefs(www.)" width="79" binding="{Binding AhrefsWww}">
<datagridtextcolumn header="Archive" width="79" binding="{Binding Archive}">

2) for cell style the links mentioned by other accepted solutions are helpful.

3) for editing a text in a cell programmatically: Click here


这篇关于在WPF项目中创建简单DataGrid并设置其单元格字体的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆