1

<UserControl>派生の要素を base の要素にバインドするにはどうすればよい<UserControl>ですか?

Tile という UserControl を基本クラスとして定義しました。XAMLから派生したオブジェクトをインスタンス化しようとしたときにXAMLがボークしない場合、これは抽象基本クラスになります...

とにかく、これまでのところ、Tile には OuterShape という 1 つの依存関係プロパティしか含まれていません。これは System.Windows.Shapes.Shape オブジェクトです (抽象的でもあります)。したがって、すべてのタイルには、レンダリングのために、それらに関連付けられたある種の視覚的な形状があります。しかし、タイルにはさまざまな種類があります --- System.Windows.Shapes.Paths を使用するもの、System.Windows.Shapes.Polygons を使用するものなどがあります。

だから今、私は最初の派生 UserControl: PolyTile に取り組んでいます。名前が示すように、これは System.Windows.Shapes.Polygon を「OuterShape」プロパティとして使用するタイルです。

私が直面している問題は、XAML<Polygon>要素を基本クラスの Shape プロパティに正常にバインドする方法がわからないことです。

/************
** Tile.cs **
************/
// This class contains a generic shape, and that's all.
// This class does NOT have a XAML file to go with it. It is purely code implemented.
public class Tile : System.Windows.Controls.UserControl
{
    public static readonly System.Windows.DependencyProperty OuterShapeProperty
        = System.Windows.DependencyProperty.Register(   "OuterShape",
                                                        typeof(System.Windows.Shapes.Shape),
                                                        typeof(Tile));
    public System.Windows.Shapes.Shape OuterShape
    {   get { return (System.Windows.Shapes.Shape)GetValue(OuterShapeProperty); }
        set { SetValue(OuterShapeProperty, (System.Windows.Shapes.Shape)value); }
    }
}

...................................

<!--*****************
*** PolyTile.xaml ***
******************-->
<local:Tile x:Name="__PolyTile__" x:Class="WPFApplication1.PolyTile"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFApplication1">
    <Grid Name="grdMain">
        <Polygon Name="OuterPoly" />
            <!--OuterPoly needs to be data-bound (OneWay)
            to the OuterShape property of the Tile UserControl.
            That way, when my derived class sets OuterShape to a new <Polygon> element,
            OuterShape will show up on the screen as a <Polygon>-->
    </Grid>
</local:Tile>

...................................

/****************
** PolyTile.cs **
****************/
// This class encapsulates a tile that is in the shape of a specified <Polygon> element.
/// <summary>
/// Interaction logic for PolyTile.xaml
/// </summary>
public partial class PolyTile : Tile // Derived from Tile
{
    public PolyTile()
    {
        InitializeComponent();

        System.Windows.Data.BindingExpressionBase BindExp;
        PolyConverter polyConverter = new PolyConverter();

        System.Windows.Data.Binding PolyBinding = new System.Windows.Data.Binding("OuterPoly");
        PolyBinding.Source = __PolyTile__;
        PolyBinding.Mode = System.Windows.Data.BindingMode.OneWayToSource;
        PolyBinding.Converter = polyConverter;
        BindExp = __PolyTile__.SetBinding(Tile.OuterShapeProperty, PolyBinding);
    }



    // The framework won't convert a base object into a derived object without an explicit cast ...
    // ... in this case, that means I have to make a custom DataConverter.
    [System.Windows.Data.ValueConversion(typeof(System.Windows.Shapes.Polygon), typeof(System.Windows.Shapes.Shape))]
    protected class PolyConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {    return value;    }
        // OneWayToSource binding: Thus the Convert method is never used.
        // Rather, ConverBack is used to cast the base <Shape> into a derived <Polygon>

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() == typeof(System.Windows.Shapes.Polygon))
                return value;
            else
                return System.Windows.DependencyProperty.UnsetValue;
        }
    }
}

XAML でこのバインディングを設定する方法がわかりません。(私はすべてを考えたと言うほどDataContextsをよく理解していませんが...)

そのため、バックコードでバインディングを手動で設定してみました。バインディング パスとソースおよびターゲット要素を整理するさまざまな方法を試しました。致命的な例外、PathError、または UpdateSourceError を取得するたびに、似たようなことが起こります。

私が間違っていることを誰かが知っていますか?
どんなアドバイスでも大歓迎です!

4

2 に答える 2

2

あなたはこれをいくつかの方法で行うことができます

相対ソースを使用できます:-

  <TextBlock
     Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ParentUserControl}}, Path=MyProperty}" />

または、読みやすいと思う要素名バインディングを使用できます。(xaml ファイルでコントロール全体に名前を付けるには、ユーザー コントロールで x:Name 構文を使用する必要があります。

  <TextBlock
     Text="{Binding ElementName=childUserControl, Path=MyProperty}" />

これは、これをテストするために私が書いた小さなテストプロジェクトです

于 2009-07-27T01:40:23.700 に答える
0

これは、XAML オブジェクト構文を使用して実行できます。

デフォルトのデータ コンテキストは完全に機能することに注意することが重要です。非常に明示的にしようとすると問題が発生します。例:バインディングに追加すると、バインディングは失敗しますElementName="__PolyTile__"

また、このソリューションではデータ コンバーターは必要ないようです。これにより、おそらく型の安全性が少し損なわれますが、コードをはるかに簡単に理解できます。

<!--*****************
*** PolyTile.xaml ***
******************-->
<local:Tile x:Name="__PolyTile__" x:Class="WPFApplication1.PolyTile"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFApplication1">

    <local:Tile.OuterShape>
        <Binding Path="OuterPoly" Mode="OneWayToSource" />
    </local:Tile.OuterShape>

    <Grid Name="grdMain">
        <Polygon Name="OuterPoly" />
    </Grid>

</local:Tile>
于 2009-07-28T06:28:26.213 に答える