我想合并两个列标题。在您将此问题标记为重复之前,请进一步阅读。我不想要超级标题。我只想合并两个列标题。看看下面的图片:
您能看到两个标题为移动电话号码 和移动号码2 的列吗?我想只显示1个列标题为移动电话号码。
以下是用于创建上述dataGrid的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>
UPDATE1:
UPDATE2
我创建了一个转换器,如下所示:
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>
我得到的输出:
UPDATE3:
最后我得到了所需的输出。
以下是Converter的代码:
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>
输出:
答案 0 :(得分:1)
试试这个
<DataGridTextColumn Header="Mobile Numbers">
<DataGridTextColumn.Binding>
<MultiBinding StringFormat=" {0} {1}" Converter="{StaticResource YourConverter}">
<Binding Path="MobileNo1"/>
<Binding Path="MobileNo2"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>