2011-10-26 45 views
0

我在網格中有一個文本框,文本框有一個應用於它的樣式,我試圖將寬度綁定到網格中的柱寬。綁定到資源字典中的網格寬度

window.xaml

<Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <Label Content="Register User" Grid.Column="0" Grid.Row="0" Style="{StaticResource LabelStyle}"/> 
    <Label Content="User Name: " Grid.Column="0" Grid.Row="1" Style="{StaticResource LabelStyle}"/> 


    <TextBox Grid.Column="1" Grid.Row="1" Style="{StaticResource TextBoxStyle}"/> 
</Grid> 

DefaultStyle.xaml

<Style TargetType="{x:Type Label}" x:Key="LabelStyle"> 
    <Setter Property="HorizontalAlignment" Value="Left"/> 
    <Setter Property="VerticalAlignment" Value="Center"/> 
</Style> 

<Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyle"> 
    <Setter Property="HorizontalAlignment" Value="Left"/> 
    <Setter Property="VerticalAlignment" Value="Center"/> 
    <Setter Property="Width" Value="{<!--?????????--!>}"/> 
</Style> 

我已經嘗試了一些東西,但不能解決如何綁定這個問題。

回答

2

而不是設置寬度,只需設置HorizontalAlignment="Stretch"

<Setter Property="HorizontalAlignment" Value="Stretch"/>