DependancyProperty
ユーザーコントロールの 1 つを の のWidth
プロパティにバインドしようとしてColumn
いGrid
ます。
次のようなコードがあります。
<Grid x:Name="MyGridName">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="TitleSection" Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>...</Grid.RowDefinitions>
<GridSplitter x:Name="MyGridSplitter" Grid.Row="0" Grid.Column="0" ... />
</Grid>
別の Usercontrol で、次のようにDependancyProperty
定義しています。
public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.Register("TitleWidth", typeof(int), typeof(MyUserControl));
public int TitleWidth
{
get { return (int)base.GetValue(TitleWidthProperty); }
set { base.SetValue(TitleWidthProperty, value); }
}
コードで Usercontrol のインスタンスを作成しているため、次のようなバインド ステートメントがあります。
MyUserControl Cntrl = new MyUserControl(/* Construction Params */);
BindingOperations.SetBinding(Cntrl , MyUserControl.AnotherProperty, new Binding { ElementName = "objZoomSlider", Path = new PropertyPath("Value"), Mode = BindingMode.OneWay });
BindingOperations.SetBinding(Cntrl , MyUserControl.TitleWidthProperty, new Binding { ElementName = "TitleSection", Path = new PropertyPath("ActualWidth"), Mode = BindingMode.OneWay });
/* Other operations on Cntrl */
定義された最初のバインディングは、実際の UIElement (この場合は Slider) へのバインディングですが、素晴らしく機能しますが、「TitleSection」(Grid で定義された ColumnDefinition) へのバインディングは失敗します。コードにブレークポイントを置き、"TitleSection" を監視すると、期待されるオブジェクトが返されます。
ax:Name'd ColumnDefinition をバインドできないのではないかと疑い始めています。グリッドの最初の列の幅の変化にどのようにバインドできるかを誰かが提案できますか?
EDIT #1 - コメントに答える
TitleWidth
プロパティのセッターにブレークポイントが設定され、GridSplitter コントロールを使用して最初の列のサイズを変更すると、ブレークポイントにヒットしないという意味で、データバインディングは「失敗」します。さらに、DependancyProperty のTitleWidth
変更が実行されないときに実行されると予想されるコード。
ユーザー コントロールが作成され、関数内のグリッド内の Stackpanel に追加されWindow_Loaded
ます。ユーザーコントロールが構築されるまでにグリッドがレンダリングされていると思います。確かに x:Name'd ElementTitleSection
は監視可能であり100
、それらが構築されているとき/バインディングが発生する前の値を持っています。
編集#2 - おそらくこれと関係がありますか?
Grid ColumnDefinition ドキュメントの MSDN ページをくまなく調べていて、 GridLength()に出くわしましたが、バインディング式でこれを使用する方法がわかりません。関連する GridLengthConverter は、IValueConverter から派生していないため、バインディング コードでコンバーターとして使用できません。
私は、Grid オブジェクト内のセルの 1 つの ActualWidth プロパティに何らかの形でバインドする方向に傾いています。列定義にバインドするほどきれいには見えませんが、現時点ではそれを機能させることができません。