У меня была эта проблема в течение некоторого времени, и я, наконец, решил ее следующим образом:
Присоединение к событию настройки диаграммы:
_chart.Customize += Remove_1899_Label;
И в обработчике события Remove_1899_Label:
private void Remove_1899_Label(object sender, EventArgs e)
{
Chart msChart = sender as Chart;
foreach (var chartArea in msChart.ChartAreas)
{
foreach (var label in chartArea.AxisX.CustomLabels)
{
if (!string.IsNullOrEmpty(label.Text) && label.Text == "1899")
{
label.SetFieldValue("customLabel", true);
label.Text = string.Empty;
}
}
}
}
NB. По какой-то причине изменение текста метки не будет работать, если вы не установите для переменной-члена private bool с именем 'customLabel' метки значение true.Реализация метода расширения 'SetFieldValue (string, object)' я получил отсюда: http://dotnetfollower.com/wordpress/2013/06/c-how-to-set-or-get-value-of-a-private-or-internal-field-through-the-reflection/
Однако для полноты я включил его ниже.
public static class ReflectionHelper
{
//...
// here are methods described in the post
// http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/
//...
private static FieldInfo GetFieldInfo(Type type, string fieldName)
{
FieldInfo fieldInfo;
do
{
fieldInfo = type.GetField(fieldName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
type = type.BaseType;
}
while (fieldInfo == null && type != null);
return fieldInfo;
}
public static object GetFieldValue(this object obj, string fieldName)
{
if (obj == null)
throw new ArgumentNullException("obj");
Type objType = obj.GetType();
FieldInfo fieldInfo = GetFieldInfo(objType, fieldName);
if (fieldInfo == null)
throw new ArgumentOutOfRangeException("fieldName",
string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName));
return fieldInfo.GetValue(obj);
}
public static void SetFieldValue(this object obj, string fieldName, object val)
{
if (obj == null)
throw new ArgumentNullException("obj");
Type objType = obj.GetType();
FieldInfo fieldInfo = GetFieldInfo(objType, fieldName);
if (fieldInfo == null)
throw new ArgumentOutOfRangeException("fieldName",
string.Format("Couldn't find field {0} in type {1}", fieldName, objType.FullName));
fieldInfo.SetValue(obj, val);
}
}