UIElement as a StaticResource
-
hi there, i was wondering on how to use my defined Button in Grid.Resources to fill my 2X2 Grid???!!!!
<Grid><
< Grid.Resources>
<Button x:Key = "btn">TEST</Button>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefenitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefenitions>
????????????????
????????????????
????????????????
</Grid>if there is no way, so {StaticResource ...} is only worked over properties! i mean : Property = {StaticResource ...}?????
-
hi there, i was wondering on how to use my defined Button in Grid.Resources to fill my 2X2 Grid???!!!!
<Grid><
< Grid.Resources>
<Button x:Key = "btn">TEST</Button>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefenitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefenitions>
????????????????
????????????????
????????????????
</Grid>if there is no way, so {StaticResource ...} is only worked over properties! i mean : Property = {StaticResource ...}?????
-
hi there, i was wondering on how to use my defined Button in Grid.Resources to fill my 2X2 Grid???!!!!
<Grid><
< Grid.Resources>
<Button x:Key = "btn">TEST</Button>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefenitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefenitions>
????????????????
????????????????
????????????????
</Grid>if there is no way, so {StaticResource ...} is only worked over properties! i mean : Property = {StaticResource ...}?????
Sorry about the previous post. You can't reuse the same instance of a button (resources are dictionaries) twice. Besides that resource extensions don't let you specify dependency properties so you won't be able to set the Grid.Row property for example. So I think you should use either styles or maybe custom controls. Here is a tested code.
<Grid>
<Grid.Resources>
<Style x:Key="HelloWorldButton" TargetType="{x:Type Button}">
<EventSetter Event="Click" Handler="Button_Click" />
<Setter Property="Content" Value="Hello, world!" />
</Style>
</Grid.Resources><Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Button Style="{StaticResource HelloWorldButton}" /> <Button Grid.Column="1" /> <Button Grid.Row="1" /> <Button Grid.Row="1" Grid.Column="1" Style="{StaticResource HelloWorldButton}" />
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
var sndr = sender as Button;
if (sndr != null)
{
var gridRow = (int)sndr.GetValue(Grid.RowProperty);
var gridColumn = (int)sndr.GetValue(Grid.ColumnProperty);
MessageBox.Show("Hello, world!" + Environment.NewLine
+ "Button at row " + gridRow + ", column " + gridColumn + " was clicked.");
}
}Eslam Afifi