2015-01-04 38 views
1

我在网格的一列中有一个组框和网格分离器控件。组框的水平对齐被设置为伸展,因此当我拖动分离器时它占据了所有空间。一切运作良好。水平拉伸元素的绑定宽度属性

现在我需要将组框的值存储在绑定对象的属性中,但只要绑定宽度属性,它就会被卡住,不再在拉伸分离器时拉伸它本身。

我知道原因,因为现在绑定的属性是它的宽度的责任,它没有得到改变。但不知道如何使它工作。这是我的XAML。

<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <Grid x:Name="InnerGrid" HorizontalAlignment="Stretch" Height="{Binding ElementName=Control1,Path=ActualHeight}"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" MinWidth="200"/> 
       <ColumnDefinition Width="Auto"/> 
      </Grid.ColumnDefinitions> 
      <GroupBox Header="{Binding TrackName}" VerticalAlignment="Stretch" Margin="3 0 3 0" HorizontalAlignment="Stretch" /> 
      <GridSplitter Width="5" VerticalAlignment="Stretch" Focusable="False" Background="Gray"/> 
     </Grid> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
+0

你需要以某种方式推动'GroupBox.ActualWidth'回到你的对象/视图模型。有人可能会认为'OneWayToSource'绑定可以完成这项工作,但遗憾的是,您不能在只读的DependencyProperty(ActualWidth)上设置任何绑定。请参阅这两个解决方法:http://stackoverflow.com/a/7227295/1869660 ..和http://stackoverflow.com/a/1083733/1869660 – Sphinxxx

+0

您的问题是不够清楚..我看到在你的代码ItemsControl模板和DataTemplate ..我怀疑你是过分简化了你在问题中遇到的问题的解释。 –

回答

0

也许你在绑定ColumnDefinition宽度真正感兴趣的,像这样:

<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="{Binding Width}" MinWidth="200"/> 
    <ColumnDefinition Width="Auto"/> 
</Grid.ColumnDefinitions> 
0

据我所知,您需要阅读GroupBox的计算宽度。您可以使用ActualWidth属性来达到此目的。

编辑: 您可以编写自定义分组框中,并利用相关属性:

public class MyGroupBox : GroupBox 
{ 
    public static readonly DependencyProperty CurrentWidthProperty = 
     DependencyProperty.Register("CurrentWidth", typeof(double), 
     typeof(MyGroupBox), new FrameworkPropertyMetadata(0d)); 

    public double CurrentWidth 
    { 
     get { return this.ActualWidth; } 
     set { SetValue(CurrentWidthProperty, value); } 
    } 
} 

XAML:

<Window x:Class="FunWithWpfAndXP.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:FunWithWp" 
     Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" MinWidth="200"/> 
      <ColumnDefinition Width="Auto"/> 
     </Grid.ColumnDefinitions> 
     <local:MyGroupBox CurrentWidth="{Binding Path=myProp}" VerticalAlignment="Stretch" Margin="3 0 3 0" HorizontalAlignment="Stretch"/> 
     <GridSplitter Width="5" VerticalAlignment="Stretch" Focusable="False" Background="Gray"/> 
    </Grid> 
</Window> 
+0

我知道我可以将网格的宽度属性与网格的实际宽度属性绑定,但是我怎样才能在我的对象属性中获得组框宽度,这是此处主要关注的问题。 – MegaMind

+0

使用依赖属性。看上面的例子 –

0

问题是你[R结合正在重置由GridSplitter改变宽度,设置的GroupBoxWidthMode结合OneWayToSource应该(可能)帮助你,你可能会得到这样的事情:

<GroupBox Width="{Binding Path=MyGroupBoxWidth, Mode=OneWayToSource}"/> 

MSDN

OneWayToSource:目标属性更改时更新源属性。

设置,这将导致在你的代码中的财产将被更新,而不是其他方式

+0

模式一种方式来源不会帮助我,因为如果我将来不能使用,节省宽度的意义何在。我的组框应该有存储在对象中的默认宽度。 – MegaMind