Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Trigger Binding to Child Control Not Working

$
0
0

I have this on a WPF Window

<local:TestControl><local:TestHost /></local:MediaPlayer>

Classes defined like this
public class TestControl : ContentControl
{
    static TestControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
        ContentProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(ContentChanged, CoerceContent));
    }

    private static void ContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var P = d as TestControl;
        P.Host = e.NewValue as TestHost;
    }

    private static object CoerceContent(DependencyObject d, object baseValue) => baseValue as TestHost;

    public static readonly DependencyPropertyKey HostProperty = DependencyProperty.RegisterReadOnly("Host", typeof(TestHost), typeof(TestControl), null);
    public TestHost Host { get => (TestHost)GetValue(HostProperty.DependencyProperty); protected set => SetValue(HostProperty, value); }
}

public class TestHost : Control
{
    static TestHost()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TestHost), new FrameworkPropertyMetadata(typeof(TestHost)));
    }

    public static readonly DependencyProperty IsPlayingProperty = DependencyProperty.Register("IsPlaying", typeof(bool), typeof(TestHost), 
        new PropertyMetadata(true));
    public bool IsPlaying { get => (bool)GetValue(IsPlayingProperty); set => SetValue(IsPlayingProperty, value); }
}

Then in Generic.xaml

<Style TargetType="{x:Type local:TestControl}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:TestControl}"><Border><Grid><ContentPresenter Content="{TemplateBinding Content}" /><Button x:Name="MyButton" Content="False" Height="50" Width="100" /></Grid></Border><ControlTemplate.Triggers><DataTrigger Value="True" Binding="{Binding Host.IsPlaying, RelativeSource={RelativeSource TemplatedParent}}"><Setter TargetName="MyButton" Property="Content" Value="True" /></DataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

It should display "true" in the button, but remains "false".

Setting a breakpoint when setting Host, it does get called and Host does get set correctly.

I'm getting this in the debug log after changing VS option to display all binding debug info.

System.Windows.Data Information: 41 : BindingExpression path error: 'Host' property not found for 'object' because data item is null.  This could happen because the data provider has not produced any data yet. BindingExpression:Path=Host.IsPlaying; DataItem=null; target element is 'TestControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=Host.IsPlaying; DataItem=null; target element is 'TestControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Host.IsPlaying; DataItem=null; target element is 'TestControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=Host.IsPlaying; DataItem=null; target element is 'TestControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Host.IsPlaying; DataItem=null; target element is 'TestControl' (Name=''); target property is 'NoTarget' (type 'Object')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Host.IsPlaying; DataItem=null; target element is 'TestControl' (Name=''); target property is 'NoTarget' (type 'Object')

How can I get this trigger binding to work?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>