0

カスタム パネルに対して次のように宣言された添付プロパティがあります。

public static readonly DependencyProperty WeightProperty = DependencyProperty.RegisterAttached(
        "Weight", typeof(double), typeof(WeightedPanel),
                new FrameworkPropertyMetadata(1.0, 
                    FrameworkPropertyMetadataOptions.AffectsParentMeasure |
                    FrameworkPropertyMetadataOptions.AffectsParentArrange ));

public static void SetWeight(DependencyObject obj, double weight)
{
    obj.SetValue(WeightProperty, weight);
}

public static double GetWeight(DependencyObject obj)
{
    return (double) obj.GetValue(WeightProperty);
}

パネルを次のように定義すると、正常に動作します。

<local:WeightedPanel Grid.Row="0" Height="200">
    <Button local:WeightedPanel.Weight="8" />
    <Button local:WeightedPanel.Weight="2"/>
</local:WeightedPanel>

しかし、このパネルを として使用ItemsPanelTemplateするListBoxと、常にデフォルト値が に返されますArrangeOverride

<ListBox Grid.Row="2" Height="100">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <local:WeightedPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <Button local:WeightedPanel.Weight ="6" />
    <Button local:WeightedPanel.Weight ="4"/>
</ListBox>

また、カスタム ラップ パネルを で使用すると、Arrange メソッドListBoxが送信されるため、Arrange で値を設定できないことにも気付きました。double.PositiveInfinite単独で使用しても同じように機能します

ありがとう

4

1 に答える 1

0

同じことを試しても、他のパネル形式ではうまくいきませんでした。グリッドをセットアップしたかったのですが、うまくいきませんでした。

問題は、ListBox は実際の論理的な子として ListBoxItem のみを持つことができ、ボタンなどではなく、ListBox のコンテンツ ペインに Button または任意の項目を追加すると、実行すると、ItemsPanel は ListBoxItem として直接の子を持ち、ListBoxItem のコンテンツは追加したコントロールになります。

したがって、実行時にはこれがビジュアルツリーになります...

ItemsControl (ListBox)
     ItemsPanel (WeightedPanel)
          ListBoxItem
              Button
          ListBoxItem
              Button...

これが、添付プロパティが機能しない理由です。

解決策は、ItemContainerStyle で ListBoxItem のプロパティを DataContext の WeightedPanel.Weight に設定しようとすることです。私はその混乱を知っています。

また

ListBoxItem を子として追加できます..のように

<ListBox>
     <ListBox.ItemsPanel>
          <ItemsPanelTemplate>
                <local:WeightedPanel />
            </ItemsPanelTemplate>        
      </ListBox.ItemsPanel>
    <ListBoxItem local:WeightedPanel.Weight="4"><Button/></ListBoxItem>
    <ListBoxItem local:WeightedPanel.Weight="4"><Button/></ListBoxItem>
</ListBox>
于 2009-10-06T06:41:42.997 に答える