gpt4 book ai didi

c# - 合并 DataGrid 列标题

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:49 25 4
gpt4 key购买 nike

我想合并两个列标题。在您将此问题标记为重复之前,请进一步阅读。我不想要 super 标题。我只想合并两个列标题。看看下面的图片:

enter image description here

你能看到标题为 手机号码 1 和手机号码 2 的两列吗?我只想在那里显示 1 个列标题作为手机号码

这是用于创建上述数据网格的 XAML:

<DataGrid Grid.Row="1" Margin="0,10,0,0" ItemsSource="{Binding Ledgers}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Customer Name" Binding="{Binding LedgerName}" />
<DataGridTextColumn Header="City" Binding="{Binding City}" />
<DataGridTextColumn Header="Mobile Number 1" Binding="{Binding MobileNo1}" />
<DataGridTextColumn Header="Mobile Number 2" Binding="{Binding MobileNo2}" />
<DataGridTextColumn Header="Opening Balance" Binding="{Binding OpeningBalance}" />
</DataGrid.Columns>
</DataGrid>

更新1:

enter image description here

更新2

我创建了一个转换器如下:

public class MobileNumberFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value != DependencyProperty.UnsetValue)
{
if (value.ToString().Length <= 15)
{
int spacesToAdd = 15 - value.ToString().Length;
string s = value.ToString().PadRight(value.ToString().Length + spacesToAdd);
return s;
}

return value.ToString().Substring(0, value.ToString().Length - 3) + "...";
}

return "";
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

我在 XAML 中使用它如下:

<DataGridTextColumn Header="Mobile Numbers">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0} {1}">
<Binding Path="MobileNo1" Converter="{StaticResource mobileNumberFormatConverter}"/>
<Binding Path="MobileNo2" Converter="{StaticResource mobileNumberFormatConverter}"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>

我得到的输出:

enter image description here

更新3:

最后我得到了想要的输出。

这是转换器的代码:

public class MobileNumberFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value != DependencyProperty.UnsetValue)
{
if (parameter.ToString().ToUpper() == "N")
{
if (value.ToString().Length <= 15)
{
return value.ToString();
}
else
{
return value.ToString().Substring(0, 12);
}
}
else if (parameter.ToString().ToUpper() == "S")
{
if (value.ToString().Length <= 15)
{
int spacesToAdd = 15 - value.ToString().Length;
string spaces = "";
return spaces.PadRight(spacesToAdd);
}
else
{
return "...";
}
}
}

return "";
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

这是我的 XAML:

<DataGridTemplateColumn Header="Mobile Numbers">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding MobileNo1, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=N}" />
<Run Text="{Binding MobileNo1, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=S}" FontFamily="Consolas"/>
<Run Text=" " FontFamily="Consolas"/>
<Run Text="{Binding MobileNo2, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=N}" />
<Run Text="{Binding MobileNo2, Converter={StaticResource mobileNumberFormatConverter}, ConverterParameter=S}" FontFamily="Consolas"/>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

输出:

enter image description here

最佳答案

试试这个

  <DataGridTextColumn Header="Mobile Numbers">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0} {1}" Converter="{StaticResource YourConverter}">
<Binding Path="MobileNo1"/>
<Binding Path="MobileNo2"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>

关于c# - 合并 DataGrid 列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25476323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com