6

バインディング/依存関係のプロパティを強制していて、概念を根本的に理解していないことに気づきました。

依存関係プロパティは次のとおりです。

public string Problem
{
    get { return (string)GetValue(ProblemProperty); }
    set { SetValue(ProblemProperty, value); }
}

public static readonly DependencyProperty ProblemProperty =
    DependencyProperty.Register(
    "Problem",
    typeof(string),
    typeof(TextBox));

XAMLは次のとおりです。

<TextBlock Text="{Binding Path=Problem}"/>

オブジェクトのコンストラクターでプロパティを手動でProblem値に設定していますが、それに応じて更新されませんTextBlock。。。何か案は?バインディングを試しましたがMode="OneWay"Mode="TwoWay"まだ機能しません。

これは自動的に機能するはずだと思いましたか?それとも私は根本的に何かが間違っていますか?

ありがとう

4

6 に答える 6

11

あなたが抱えている問題は、間違いなく DataContext に関連しています。{Binding} 拡張機能は、バインド先のプロパティが存在する場所を知る必要があります。参照するデフォルトの場所は要素 DataContext であり、デフォルトでは常に親要素の DataContext に設定されます。DataContext を論理ツリーで親ウィンドウまでたどると、DataContext は null になります (ウィンドウの DataContext が null であるため)。したがって、テキストブロックの {Binding} は、「Text プロパティを DataContext の問題のプロパティにバインドします...これは null です。

これを解決するにはいくつかの方法があります。1つは、Jobiが述べたように行い、バインディングのElementプロパティを設定して、DependencyPropertyが次のように定義されているウィンドウを指すようにすることです。

<TextBlock Text="{Binding Path=Problem,ElementName=_window}" />

別のオプションは、ウィンドウの DataContext をそれ自体を指すように設定することです。そうすれば、そのコンテンツに含まれるすべての要素がすべてウィンドウの DataContext を持つことになります。

<Window ....
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

ウィンドウで定義されたプロパティ (Problem 依存プロパティなど) にバインドする必要がある場合はいつでも、次のように実行できます。

<TextBlock Text="{Binding Problem}" />
于 2008-11-25T16:35:23.243 に答える
2

ここでElementNameBindingを使用できます。要素は、ウィンドウ自体になります。

<Window x:Class="WpfToolTip.Window1"
x:Name="_window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Click="OnClick" Content="OK" />
        <Button Click="OnCancel" Content="Cancel" />
        <TextBlock Text="{Binding Path=Problem,ElementName=_window}" />
</StackPanel>

于 2008-11-25T18:10:38.947 に答える
2

typeof(object)の代わりに を試してくださいtypeof(string)

于 2012-03-22T10:07:41.623 に答える
0

コードでは、クラス TextBox (引用の最後の行) の依存関係プロパティを登録します。

public static readonly DependencyProperty ProblemProperty =
DependencyProperty.Register(
"問題",
typeof(string),
typeof(TextBox));

そのため、テキスト ボックスに対してのみ ProblemProperty の値を設定できますが、どのコード スニペットにもテキスト ボックスが見つかりません。値が割り当てられるタイプの依存関係プロパティを登録する必要があります。サンプルから、正しい選択は私には明らかではありません。Micah のように、それをウィンドウの DP として定義し、インスタンス化されたウィンドウにプロパティを設定できます。または、ウィンドウ内の任意の名前付き依存オブジェクト、つまり Name=m_ContentElement を持つオブジェクトに定義し、バインディングを
{Binding ElementName=m_ContentElement, Path=Problem}
以下に設定することもできます。
{Binding Problem, ElementName=m_ContentElement}

于 2009-02-23T21:12:01.877 に答える
0

これがセットされているウィンドウです。

public partial class Window1 : Window
{
    public string Problem
    {
        get { return (string)GetValue(ProblemProperty); }
        set { SetValue(ProblemProperty, value); }
    }

    public static readonly DependencyProperty ProblemProperty =
                    DependencyProperty.Register(
                    "Problem",
                    typeof(string),
                    typeof(Window1));


    public Window1()
    {
        InitializeComponent();

        Problem = "ifowiof";
    }

    public void OnClick(object sender, EventArgs e)
    {
        Problem = "behl";
    }

    public void OnCancel(object sender, EventArgs e)
    {
       Problem = "eioeopje";
    }
}

XAML:

<Window x:Class="WpfToolTip.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
            <Button Click="OnClick" Content="OK" />
            <Button Click="OnCancel" Content="Cancel" />
            <TextBlock Text="{Binding Path=Problem}" />
    </StackPanel>
</Window>

RelativeSourceロード時にあなたが言ったように設定すると機能しますがProblem、コードでプロパティを手動で(つまり、ボタンをクリックして)変更するTextBlockと、新しい値で更新されません。

于 2008-11-25T17:30:14.660 に答える
0

あなたが説明した問題の理由を理解するには、2 つの方法があります。

最初に、(依存関係プロパティの宣言で) プロパティ変更ハンドラーを設定し、そこにブレークポイントを配置する必要があります。プロパティが変更されているかどうかがわかります。

次に、依存関係プロパティの所有者のタイプを確認する必要があります。

完全な xaml とコード ビハインドを表示していただけますか?

于 2010-08-26T12:57:01.453 に答える