I have a button in a grid. The size of the button is a component in the grid layout. I want to change the content of the button programmatically without the button resizing and causing a re-layout of the grid that contains it.
This is the XAML for the grid:
<Grid Grid.Row="0" Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.ColumnSpan="4" Grid.Row="0" Style="{StaticResource IfStyle}">Don't have Apache Ant yet?</Label>
<Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right">Download URL:</Label>
<TextBox Name="TxtDownloadUrl" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1" VerticalAlignment="Center" />
<Label Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Right">Destination Directory:</Label>
<TextBox Name="TxtDestinationDir" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Center" Margin="0,0,10,0" />
<Button Grid.Row="2" Grid.Column="3" VerticalAlignment="Center" Name="BtnBrowseDestination" HorizontalAlignment="Stretch">...</Button>
<Canvas Name="ProgressCanvas" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3" Height="58" Margin="10" SizeChanged="OnCanvasResize">
<Image Canvas.Left="4" Canvas.Top="4" Source="/VSADTWizards;component/images/Globe.png" Stretch="Fill" Height="48" Width="48" />
<Image Canvas.Right="4" Canvas.Top="4" Source="/VSADTWizards;component/images/Computer.png" Stretch="Fill" Height="48" Width="48" />
<Label Canvas.Left="60" Canvas.Top="4" Name="LabelTotalBytes" />
<Label Canvas.Right="60" Canvas.Top="4" Name="LabelReceivedBytes" />
</Canvas>
<Button Name="BtnInstall" Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Right" Padding="10">Download, Unzip and Install</Button>
</Grid>
To change the content, I'm doing a simple content update like this:
BtnInstall.Content = "开发者_StackOverflowCancel Download";
// or
BtnInstall.Content = "Download, Unzip and Install";
I want to change the "Download, Unzip and Install" text to "Cancel Download" and back without a resize.
I don't want to set min/max sizes because I want the button to render correctly if someone has a different DPI setting or language or whatever. I do think that freezing the size after it is initially laid out would be ok but I haven't been able to figure out how to do that.
If you want to freeze the size after the button has been rendered, you could handle its Loaded event, then set the buttons Width equal to its ActualWidth at that point.
Colin E.
After finished loading the Element, you can save the Width and Height from the Button as fixed values:
Button.Height = Button.ActualHeight;
Button.Width = Button.ActualWidth;
if it's not working I suggest saving it in some properties and after changing the Content you can set the saved Width and Height to the button.
精彩评论