Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding dependency property using XAML
    primarykey
    data
    text
    <p>Inside my user control MainControl I have dependency property named Repository. I want to bind this repository property in MainWindow using xaml</p> <pre><code>&lt;my:MainControl x:Name="mainControl" Repository="{Binding }" Visibility="Visible" /&gt; </code></pre> <p>and inside MainWindow.xaml.cs I have</p> <pre><code>public MainWindow() { InitializeComponent(); _repository = new CarRepository(); DataContext = _repository; } </code></pre> <p>Inside MainControl constructor I was thinking to display some text using </p> <pre><code>public MainControl() { InitializeComponent(); lblCountCars.Content = string.Format("there is {0} cars.", Repository.CountAllCars()); } </code></pre> <p>Am I doing something wrong here <code>Repository="{Binding }"</code>? When I register user control inside code behind everything is ok but I want to learn how to do this using xaml.</p> <p><strong>Update</strong> To make it clear. I have <code>MainWindow</code> which uses two user controls. <code>MainControl</code> and <code>TreassureControl</code>. I want to send repository which is of type <code>ICarRepository</code> to any of this controls, so I created DependencyProperty named <code>Repository</code> of type <code>ICarRepository</code> both in <code>MainControl</code> and <code>TreassureControl</code>.</p> <p>My goal is to send repository instance to <code>MainControl</code> <code>Repository</code> property (DP) and to print on label Content property <code>lblCountCars.Content = Repository.CountAllCars();</code> </p> <p>Also I want this repository instance in user control to further work, not only to show simple text.</p> <p>So I tried with suggestions bellow</p> <p>MainWindow.xaml</p> <pre><code> &lt;my:MainControl x:Name="mainControl" Repository="{Binding Repository}" /&gt; </code></pre> <p>MainWindow.xaml.cs</p> <pre><code>private ICarRepository _repository; public MainWindow() { InitializeComponent(); _repository = new CarRepository(); DataContext = _repository; } </code></pre> <p>MainControl.xaml</p> <pre><code>&lt;UserControl x:Name="mainControl"&gt; &lt;Label Name="lblCountBooks" Content="{Binding ElementName=mainControl, Path=Repository.CountAllBooks()}" &lt;ItemsControl ItemsSource="{Binding ElementName=mainControl, Path=Repository}" /&gt; </code></pre> <p>MainControl.xaml.cs</p> <pre><code>public static readonly DependencyProperty RepositoryProperty = DependencyProperty.Register( "Repository", typeof(ICarRepository), typeof(MainControl), new PropertyMetadata(null)); public ICarRepository Repository { get { return (ICarRepository)GetValue(RepositoryProperty); } set { SetValue(RepositoryProperty, value); } } </code></pre> <p>label content is not updated with expected content.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload