次の 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" オブジェクトに移動しようとしましたが、役に立ちませんでした...うまくいきませんでした。
バインディングの何が問題になっていますか? 助けてくれてありがとう。