2016-06-14 42 views
-2

我有一些label「我的文本」。 我有一些UserControl,裏面有文字(「文字部分」)和一些視覺部分靠近文字。在wpf中更改控制中心

左圖是我想要實現的。正是我得到的。

我想要中心我的UserControl不是其默認中心,而是自定義點。 由於「我的文本」的長度可能有所不同,因此它應該相對於我的控件「文本部分」中心居中。

enter image description here

目標^                                                                                      當前^

+0

請發表您已經嘗試代碼爲他人檢查 –

回答

0

snapshot

您需要定義單獨的列,其中一個是中心爲您居中文本,另一個用於您視覺標籤。

現在棘手的部分是同步UserControl內的一個列和外部的一個列。您可以使用SharedSizeGroups對於這一點,就像在這個例子中,我只是用一個ContentControl中,而不是用戶控件的方便:

<Grid IsSharedSizeScope="True"> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid HorizontalAlignment="Stretch" Background="Red"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" SharedSizeGroup="colLeft"/> 
     <ColumnDefinition Width="Auto" SharedSizeGroup="colRight"/> 
    </Grid.ColumnDefinitions> 
    <Label HorizontalAlignment="Center" Background="LightBlue">MyText</Label> 
    </Grid> 
    <ContentControl Grid.Row="1"> 
    <Grid HorizontalAlignment="Stretch" Background="Yellow"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" SharedSizeGroup="colLeft"/> 
      <ColumnDefinition Width="Auto" SharedSizeGroup="colRight"/> 
     </Grid.ColumnDefinitions> 
     <Label HorizontalAlignment="Center" Background="Lime">text part a little longer 
     </Label> 
     <Label Grid.Column="1">someVisualLabel</Label> 
    </Grid> 
    </ContentControl>