0

I've got following (simple) UserControl:

XAML:

<UserControl x:Class="WpfUserControls.Controls.TextBlockUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-WpfUserControls.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Name="TextBlockUserControl">
    <UserControl.Resources>
        <ResourceDictionary Source="../Resources/ResourceDictionary.xaml"/>
    </UserControl.Resources>
    <Grid>
        <TextBlock Text="{Binding ElementName=TextBlockUserControl, Path=Label}" Visibility="{Binding ElementName=TextBlockUserControl, Path=Visible, Converter={StaticResource BoolToVis}}"/>
    </Grid>
</UserControl>

Code behind:

using System.Windows;
using System.Windows.Controls;

namespace WpfUserControls.Controls
{
    public partial class TextBlockUC : UserControl
    {
        public TextBlockUC()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(TextBlockUC), new PropertyMetadata(""));

        public bool Visible
        {
            get { return (bool)GetValue(VisibleProperty); }
            set { SetValue(VisibleProperty, value); }
        }

        public static readonly DependencyProperty VisibleProperty =
            DependencyProperty.Register("Visible", typeof(bool), typeof(TextBlockUC), new PropertyMetadata(true));
    }
}

Now I want to do the following

<uc:TextBlockUC Grid.Row="2" Grid.Column="8" Label="{Binding Test}" Visible="{Binding TransportersOnly}"></uc:ToolboxButtonUC>

Where <uc:> is a reference to the WpfUserControls namespace (obviously) and Test and TransportersOnly are properties from my ViewModel.

Any ideas why this doesn't work ?

Hans
  • 23
  • 5
  • `DataContext = this` in the UserControl's constructor effectively prevents that `ButtonText="{Binding Test}"` uses the intended, inherited DataContext. Just remove the line. – Clemens Apr 25 '18 at 07:08
  • Besides that, it's unclear how TextBlockUC and ToolboxButtonUC are related. – Clemens Apr 25 '18 at 07:11
  • @Clemens my apologies, ToolboxButtonUC was the result of a wrong copy/paste. I've corrected it in my question – Hans Apr 25 '18 at 07:44
  • 1
    @Clemens Thanks a lot for helping me out. I was so focused on the dependency properties that I did not see the obvious. – Hans Apr 25 '18 at 07:46

0 Answers0