0

I use a custom control that has a dependencyproperty Title that binds to a label within. I can set the Title property in xaml with a string but not with a {Binding title}. If I use a <Button content={Binding title} that works.

I really dont know where to look on this one as i guess string is string .. ?

WORKS
<Button Content="{Binding title}" Click="Button_Click_2">

WORKS

<PluginInterface:uc_item Title="TEST" OnClick="Button_Click_2"></PluginInterface:uc_item>

DOES NOT WORK
<PluginInterface:uc_item Title="{Binding title}" OnClick="Button_Click_2"></PluginInterface:uc_item>

THE DEPENDENCY PROPERTY

 public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set
            {

                SetValue(TitleProperty, value);
            }
        }
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(uc_item),
        new FrameworkPropertyMetadata(default(string),
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

THIS IS THE CONTAINER FOR THE CONTROLS


<ItemsControl Name="ic_searchengines" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>

IM SETTINGS THE ITEMSSOURCE OF THE ITEMSCONTROL IN ANOTHER CLASS

searchengines = new ObservableCollection<searchengine>();
main.ic_searchengines.ItemsSource = searchengines;                                                      

the binding in the first example with the button does show the bound text in the content; in the second example, setting a string on the cusotm user control shows the text on the usercontrol; third example, binding equivalent to the first button does show blank on the usercontrl.

Thanks! Looking forward

l2rek
  • 32
  • 1
  • 5
  • You may have explicitly set the control's DataContext in its XAML or code behind. That breaks any DataContext based Bindings of the properties of your control. Are there any data binding error messages in the Output Window in Visual Studio when you run the application in the debugger? – Clemens Aug 14 '19 at 19:56
  • thanks! yes i had error messages and a with relative binding to self on the user control (because i needed to feed it different properties on the outside but have just one dependencyproperty for it in the codebehind) I have now added DataContext="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=local:uc_item}}" to the usercontrol within. Thanks! – l2rek Aug 14 '19 at 21:44

1 Answers1

0

there was a datacontext set wihtin the custom user control - to feed the controls within via binding with the properties in codebehind. i had to remove it so the proper datacontext could be set on the outside ( where multiple instances use bindings to different properties) and have added a modified version with findancestor to the single controls within: DataContext="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=local:uc_item}}"

l2rek
  • 32
  • 1
  • 5