我在这里查看了一些不同的方法,但是在尝试将标签背景颜色从一个类更改为另一个页面时没有成功
我有一个名为 lbConnectionStatus 的 ServerSettings.xaml 标签我也有 Content\ServerSettings.xmal.cs 我有一个名为 SQLConnectionClass.cs 的单独类,它根据成功/失败更改标签背景颜色
这是我的 SQLConnectionClass.cs
class SQLConnectionClass
{
public void TestConnectivity(String server, String user, String pass, String db)
{
string ConnectionString = @"Data Source=" + server +
";Initial Catalog=" + db +
";User ID=" + user +
";Password=" + pass;
SqlConnection Connection = new SqlConnection(ConnectionString);
try
{
Connection.Open();
Connection.Close();
success();
}
catch (Exception ex)
{
failed(ex.Message.ToString());
}
}
void success()
{
MessageBox.Show("Success function","Message");
Content.ServerSettings mine = new Content.ServerSettings();
mine.lbConnectionStatus.Background = Brushes.Green;
}
void failed(string msg)
{
MessageBox.Show(msg,"Message");
Content.ServerSettings mine = new Content.ServerSettings();
mine.lbConnectionStatus.Background = Brushes.Red;
}
}
我试过做以下没有运气
Content.ServerSettings mine = new Content.ServerSettings();
mine.lbConnectionStatus.Background = Brushes.Green;
这是 Content\ServerSettings.xmal.cs 的代码
namespace SQLServer.Content
{
/// <summary>
/// Interaction logic for Server_Settings.xaml
/// </summary>
public partial class ServerSettings : UserControl
{
public ServerSettings()
{
InitializeComponent();
}
private void btTest_Click(object sender, RoutedEventArgs e)
{
SQLConnectionClass con = new SQLConnectionClass();
con.TestConnectivity(tbServer.Text, tbUsername.Text, tbPassword.Text, tbDatabase.Text);
}
}
}
这是实际 ServerSettings.xaml 的代码
<UserControl x:Class="SQLServer.Content.ServerSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel>
<Label Content="Server Address & Instance Name" />
<TextBox x:Name="tbServer" Text=".\Autobase" Margin="0,10"/>
<Label Content="Database Name" />
<TextBox x:Name="tbDatabase" Text="ABSystem7" Margin="0,10"/>
<Label Content="User-name" />
<TextBox x:Name="tbUsername" Text="sa" Margin="0,10"/>
<Label Content="Password" />
<TextBox x:Name="tbPassword" Text="user" Margin="0,10"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Connection Status" Margin="0,10"/>
<Label x:Name="lbConnectionStatus" Grid.Row="0" Grid.Column="2">
<Border Margin="0,12" BorderBrush="White" Background="Transparent" CornerRadius="15" BorderThickness="2,2,2,2" Height="15" Width="15" ClipToBounds="True"/>
</Label>
</Grid>
<TextBlock x:Name="tbMessage" Text="TESTING THIS CRAP OUT TO MAKE SURE THAT IT WORKS THE WAY THAT IT IS SUPPOSED TO BUT I AM NOT REALLY SURE WTF I AM DOING BUT HEY I CAN AT LEAST TRY TO MAKE IT WORK CORRECTLY" TextWrapping="Wrap" />
<Button x:Name="btTest" Content="Test Connection" VerticalAlignment="Bottom" Margin="0,20" Click="btTest_Click"/>
</StackPanel>
</Grid>
</UserControl>