1

次の XAML があります。

<UserControl.Resources>

<DataTemplate x:Key="ExpanderTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
        <Rectangle Stroke="Black" StrokeThickness="3" Width="2" Height="6" Fill="Black" />
    </Grid>
</DataTemplate>

<DataTemplate x:Key="CollapserTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
    </Grid>
</DataTemplate>

</UserControl.Resources>

<Grid>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Path=Name}" />
    <Grid>
        <ContentPresenter x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
        <ContentPresenter x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />
    </Grid>
</StackPanel>
</Grid>

ご覧のとおり、これは基本的に、コンテンツが名前にバインドされたテキストブロックであり、ソース クラスのいくつかの Visibility オブジェクトに可視性がバインドされた 2 つのコンテンツ プレゼンターです。ソース クラスは次のようになります。

public class MyViewModel
{
public string Name { get; set; }

public Visibility CollapserVisibility
{
    get
    {
        if (IsExpandable && IsExpanded)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }
}

public Visibility ExpanderVisibility
{
    get
    {
        if (IsExpandable && !IsExpanded)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }
}

public bool IsExpanded { get; set; }
public bool IsExpandable { get; set; }
}

私が経験している問題は、Visibility オブジェクトとのバインディングが発生しないことです。Name 文字列とのバインドは実際に行われ、(視覚的にも、拡張て getter にブレークポイントを配置することによっても) それを確認しましたが、CollapserVisibility オブジェクトと ExpanderVisibility オブジェクトの getter にブレークポイントを配置すると、それらのブレークポイントは取得されません。打つ。どうしてこれなの?また、Visual Studio の出力ウィンドウにバインディング エラーが表示されないため、さらに混乱するため、バインディングが正しく設定されているかのように動作します。

ContentPresenter の Visibility プロパティをバインドすることはできませんか? また、Visibility バインディングをデータ テンプレート (XAML 内) の "Grid" オブジェクトに移動しようとしましたが、役に立ちませんでした...うまくいきませんでした。

バインディングの何が問題になっていますか? 助けてくれてありがとう。

4

3 に答える 3

9

をa に変更するContentPresenterContentControl機能します (可視性プロパティにバインドするという点で - VM が変更を通知しないという事実は無視しています):

<ContentControl x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
<ContentControl x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />

ContentPresenterのテンプレート内で使用されることになっているという事実を除けば、これがなぜなのか、私には本当に説明できませんContentControl。しかし、私にとっては、それでも機能するはずです。これを理解するには、Reflector ベースの調査が必要になると思います。

于 2010-09-03T15:56:10.767 に答える
1

Are you ever putting any content into the contentpresenter? It might not be showing anything because there's nothing to show. The datatemplate dictates how the data should look, but you need to actually put data into it first.

それとは別に、バインドは初期値に対して機能するように見えますが、IsExpandable または IsExpanded プロパティが変更されると更新されません。

MultiDataTrigger を使用して可視性を制御する方が、おそらくはるかに優れているでしょう。

于 2010-09-03T15:53:56.587 に答える