2016-03-28 7 views
-2

Ich versuche Hintergrundfarbe der Schaltfläche (Namen: b0, b1, b2, b3, b4, b5, b6, b7, b8, b9) in WPF zur Laufzeit festzulegen.Festlegen der Hintergrundfarbe der Schaltfläche in WPF zur Laufzeit

Farbname wird jetzt von der Datenbank, die rot ist. Aber es gibt mir System.NullReferenceExceptionn: Objektverweis nicht auf eine Instanz eines Objekts

private void ButtonBgColor() 
{ 
string qryBgColor = "select Name from lookup where code in (select VALUE from qSettings where name='BUTTON_BG_COLOR') and type='BgColor'"; 
try 
{ 
sqlConnection.Open(); 
sqlCommand = new SqlCommand(qryBgColor, sqlConnection); 
sqlDataReader = sqlCommand.ExecuteReader(); 
if (sqlDataReader.Read()) 
{ 
string BUTTON_BG_COLOR = sqlDataReader["Name"].ToString(); 
Button[] b = new Button[9]; 
for (int i = 0; i < b.Length; i++) 
{ 
var textBlock08 = (TextBlock)b[i].Template.FindName("myTextBlock", b[i]); 
textBlock08.Background = (System.Windows.Media.SolidColorBrush)new System.Windows.Media.BrushConverter().ConvertFromString(BUTTON_BG_COLOR); 
} 
} 
} 
catch (Exception exp) 
{ 
MessageBox.Show(exp.ToString(), "Button Background Color Exception"); 
} 

Kann jemand mir helfen, dieses Problem zu lösen?

Vielen Dank im Voraus

Antwort

1

Sie haben nichts zu b zugeordnet. Es ist nur ein leeres Array. Daher wird das Aufrufen von b [i] immer zu einem Null-Verweis führen.

+0

es ist in Schleife, wie es NULL sein könnte? –

+0

Ihr Array ist nicht null, aber jedes Element im Array ist null. – Jace

+0

b0 ist der Name der Schaltfläche –

Verwandte Themen