我试图在鼠标悬停时修改Button的Text颜色,从黑色到另一种颜色,我使用了这个样式:
protected void Page_Load(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(Request.QueryString["srch"]))
{
String srch = Request.QueryString["srch"];
search.Text = Request.QueryString["srch"];
//perform search and display results
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["GroupsConnString"].ToString();
conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
conn.Open();
queryStr = "";
queryStr = "SELECT g.*,CONCAT(firstname,' ',lastname) as name FROM app_groups.groups g LEFT JOIN users u ON u.id = g.id_user WHERE group_name LIKE '%" + srch + "%' OR group_type LIKE '%" + srch + "%'";
cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
reader = cmd.ExecuteReader();
gname = "";//group name
gtype = "";//group type
uname = "";//user name
id = "";
warning = "";
id_owner = "";
if (reader.HasRows)
{
while (reader.HasRows && reader.Read())
{
uname = reader.GetString(reader.GetOrdinal("name"));
gname = reader.GetString(reader.GetOrdinal("group_name"));
gtype = reader.GetString(reader.GetOrdinal("group_type"));
id = reader.GetString(reader.GetOrdinal("id"));
id_owner = reader.GetString(reader.GetOrdinal("id_user"));
warning = reader.GetString(reader.GetOrdinal("warning"));
Panel pan = new Panel();
pan.CssClass = "col-sm-4 group";
if ((Session["uid"] != null && Session["uadmin"].Equals("1")) || id_owner.Equals(Session["uid"]))
{
adminCtrls = new Panel();
adminCtrls.CssClass = "adminCtrls";
btn = new Button();//delete group button for admins only
btn.Click += new EventHandler(deleteGroup);
btn.ID = "sterge";
btn.CssClass = "btn btn-danger btn-xs";
btn.Text = "Sterge grup";
btn.CommandArgument = id;
//btn.Attributes.Add("style", "float:right;");
adminCtrls.Controls.Add(btn);
if (Session["uadmin"].Equals("1"))
{
if (!warning.Equals("1"))
{
btn = new Button();//warn owner button for admins only
btn.Click += new EventHandler(warnOwner);
btn.ID = "warn";
btn.CssClass = "btn btn-warning btn-xs";
btn.Text = "Avertizeaza owner grup";
btn.CommandArgument = id;
adminCtrls.Controls.Add(btn);
}
else
{
btn = new Button();//warn owner button for admins only
btn.ID = "warn";
btn.Enabled = false;
btn.CssClass = "btn btn-warning btn-xs";
btn.Text = "A fost avertizat";
adminCtrls.Controls.Add(btn);
}
}
}
if (adminCtrls != null)
{
pan.Controls.Add(adminCtrls);
}
pan.Controls.Add(new LiteralControl("<span class='title text-center'>" + gname + "</span><br/>"));
pan.Controls.Add(new LiteralControl("<span>Owner: <span class='owner'>" + uname + "</span></span><br/>"));
pan.Controls.Add(new LiteralControl("<span>Categorie: <span class='type'>" + gtype + "</span></span><br/>"));
groupsShow.Controls.Add(pan);
}
}
reader.Close();
conn.Close();
}
}
但是这段代码的问题在于我得到悬停的结果(悬停时的颜色已经改变),但是按钮的文本没有显示在左中心,这就是结果:
这是我的代码,要在按钮上应用样式:
<Style x:Key="ButtonmenuStyle"
TargetType="Button">
<Setter Property="Foreground" Value="#393185"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<ContentPresenter x:Name="Content1"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
请如何更正我的代码,让文本以Left为中心 谢谢你的帮助
答案 0 :(得分:1)
如果要求垂直居中按钮内容,可以执行以下操作:
<ContentPresenter x:Name="Content1" VerticalAlignment="Center"/>