问题
I created button in a XAML which contains image and text, I used Grid inside a button for that. Here is my XAML code:
<Button x:Name="btnAddNewItem"
Grid.Column="0" Grid.Row="0"
FontSize="15"
BorderThickness="1.5"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Foreground="White"
Background="White"
BorderBrush="#0091EA" Margin="5,0,0,0" Height="90" Width="90">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Button.Template>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80*">
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<Image Source="/MyProject.Wpf;component/Icons/customer-icon.png" Margin="10" Grid.Row="0" Width="Auto"/>
<TextBlock Foreground="Black" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" >Customer</TextBlock>
</Grid>
</Button>
Here is screenshoot of my button, and how that acctualy looks when my app is runned:
As you can see guys, there are image and text inside of my button.
Now I'm trying to make same-look button but from code behind and I achieved this (here is image):
It's almost the same button but It's missing image+text which should be placed in Grid ..
Here is my code :
Image imgControl = new Image(); // place where I should keep my icon
TextBlock text = new TextBlock(); // place where should be text below my icon
imgControl.Source = new ImageSourceConverter().ConvertFromString(uriSource) as ImageSource;
text.Text = "Test text";
string uriSource = new Uri(@"C:\Projects\MyProject.Wpf\Icons\customer-icon.png", UriKind.Relative).ToString();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush = (SolidColorBrush)(new BrushConverter().ConvertFrom("#0091EA"));
Button a = new Button();
a.BorderThickness = new Thickness(1);
a.Background = Brushes.Transparent;
a.Foreground = new SolidColorBrush(Colors.Black);
a.BorderBrush = mySolidColorBrush;
a.Width = 90;
a.Height = 90;
a.Style = Application.Current.Resources["MyButtonStyle"] as Style;
Grid grid1=new Grid(); // I'm trying to add grid1 with two rows where I should place image and text, [row 0] for image, [row 1] for text
RowDefinition rowImage = new RowDefinition(); //creating row one
RowDefinition rowTitle = new RowDefinition(); //creating row two
rowImage.Height = new GridLength(8.0, GridUnitType.Star); //Image will take 80% of space
rowTitle.Height = new GridLength(2.0, GridUnitType.Star); //Text will take 20% of space
grid1.RowDefinitions.Add(rowImage); //Adding image to row
grid1.RowDefinitions.Add(rowTitle); //Adding text to row
Grid.SetRow(imgControl, 0);
Grid.SetRow(text, 1);
a.Content = grid1;
As you can see, there is no image, neither text placed in my button, even if it looks like I added them to a grid which is added to my button.
So I guess I did something wrong with adding grid to a button..
Thanks guys, Cheers
EDIT FOR Ed:
switch (buttonPurpose)
{
case SettingsSubmenuItemEnum.Test:
{
button.HasIcon = true;
button.Icon = Controls.Enumerations.IconType.Other;
button.Click += EH_testButton_Click;
button.IconAlignHorizontal = HorizontalAlignment.Left;
button.TextAlignHorizontal = HorizontalAlignment.Right;
break;
}
case SettingsSubmenuItemEnum.Groups:
{
if (!OperatorController.HasOperatorAccess(currentOperator)
return;
button.HasIcon = true;
button.Icon = Controls.Enumerations.IconType.Grupe;
button.Click += EH_SubmenuButtonClickGrupePromet;
button.IconAlignHorizontal = HorizontalAlignment.Left;
button.TextAlignHorizontal = HorizontalAlignment.Right;
break;
}
case SettingsSubmenuItemEnum.Cities:
{
if (!OperatorController.HasOperatorPristup(currentOperator))
return;
button.HasIcon = true;
button.Icon = Controls.Enumerations.IconType.Other;
button.Click += EH_MjestaClick;
button.IconAlignHorizontal = HorizontalAlignment.Left;
button.TextAlignHorizontal = HorizontalAlignment.Right;
break;
}
spContentSubmenu.Children.Add(button);
}
}
In code above I'm checking which button user clicked and by that I'm showing to him corresponding buttons..
variable buttonpurpose
is SettingsSubmenuItemEnum
type
回答1:
So I guess I did something wrong with adding grid to a button..
You need to add the Image and the TextBlock to the Grid's Children collection:
...
Grid.SetRow(imgControl, 0);
Grid.SetRow(text, 1);
//ADD:
grid1.Children.Add(imgControl);
grid1.Children.Add(text);
a.Content = grid1;
来源:https://stackoverflow.com/questions/41592928/how-to-add-grid-with-two-rows-and-a-content-to-a-button-programatically