这是我的Datagrid,我如何确定列之间的距离相同,以便列标题不会像第二列或第三列那样啄...
XAML:
<Style x:Key="Datag1" TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle" Value="{DynamicResource chs1}"></Setter>
</Style>
<Style x:Key="chs1" TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Brown"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="15"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
<DataGrid Name="DataGridView" Style="{DynamicResource Datag1}" Margin="0,105,0,0" FontSize="15" CanUserSortColumns="True"
Background="Azure" RowBackground="Aqua" AlternatingRowBackground="DarkGray" ColumnHeaderHeight="45" Foreground="Brown"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="True" CanUserReorderColumns="False" BorderBrush="Black">
</DataGrid>
编辑:
最佳解决方案:“您最好将DataGrid的AutoGenerateColumns属性设置为false,并明确定义每个列,并为XAML标记中的每个列设置单独的宽度。有关示例,请参阅我编辑的答案。” - mm8
非常感谢大家!
答案 0 :(得分:1)
您可以处理public void sendEmail(String fromEmail, String fromDisplayName, List<String> toList, List<String> ccList,
String subject, String emailBody, String filePhysicalPath, String fileName, String fileContentType)
throws Exception {
Transport transport = null;
try {
initConfig();
MimeMultipart multipart = new MimeMultipart();
Authenticator authenticator = new SMTPAuthenticator();
MailSSLSocketFactory sslSocketFactory = new MailSSLSocketFactory();
MimeBodyPart bodyPart = new MimeBodyPart();
String html = "";
Properties props = System.getProperties();
setProps(props);
sslSocketFactory.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sslSocketFactory);
Session session = Session.getInstance(props, authenticator);
// session.setDebug(true);
emailBody = emailBody + "<br/><br/>مرسل بواسطة : " + fromDisplayName;
html = "<html><body style='text-align:right'> " + emailBody + " </body></html>";
bodyPart.setContent(html, "text/html; charset=UTF-8");
multipart.addBodyPart(bodyPart);
MimeMessage message = new MimeMessage(session);
// attach file
BodyPart mimeBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePhysicalPath);
mimeBodyPart.setDataHandler(new DataHandler(source));
mimeBodyPart.setFileName(MimeUtility.encodeText(fileName, "utf-8", "B"));
multipart.addBodyPart(mimeBodyPart);
// end of file attach
message.setSubject(subject, "UTF-8");
message.setContent(multipart);
message.setFrom(new InternetAddress(senderEmail, fromDisplayName));
message.setReplyTo(new Address[] { new InternetAddress(fromEmail) });
transport = session.getTransport(emailProtocol);
transport.connect(senderEmail, senderPassword);
if (toList != null && toList.size() > 0) {
for (String to : toList) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
if (ccList != null && ccList.size() > 0) {
for (String cc : ccList) {
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
message.setSentDate(new Date());
transport.sendMessage(message, message.getAllRecipients());
}
}
}
} else {
throw new Exception("List of users to send email to is empty");
}
} catch (Exception ex) {
throw ex;
} finally {
if (transport != null)
transport.close();
}
}
事件并指定列所需的任何AutoGeneratingColumn
:
Width
如果要为每个列的宽度添加特定偏移量,您可以处理const double fixedWidth = 100;
private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Width = new DataGridLength(fixedWidth);
}
事件:
AutoGeneratedColumns
如果您不想为每列添加偏移量,最好将private void dg_AutoGeneratedColumns(object sender, EventArgs e)
{
DataGrid dg = sender as DataGrid;
foreach (var column in dg.Columns)
{
column.Width = column.ActualWidth + 10;
}
}
的{{1}}属性设置为AutoGenerateColumns
并明确定义每列并为其设置单独的宽度XAML标记中的每一个:
DataGrid
答案 1 :(得分:0)
您的列看起来像这样,因为它们只占用 0边距所需的空间。因此,您需要做的就是在DataGridColumnHeader的样式中设置一个新模板,更改ContentPresenter的Margin以满足您的需求(我将其设置为10,2但您可以测试不同的值以查看它的外观等)。
尝试以下代码:
<Style x:Key="chs1" TargetType="DataGridColumnHeader">
<Setter Property="Background" Value="Brown"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="10,2"
/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
</Style.Triggers>
</Style>