Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Data Binding- Binding Obj. inside Obj. to UserControl
    primarykey
    data
    text
    <p>I have 2 user controls in <code>MainWindow.xaml</code>, They are <code>SidePannel</code> and <code>Description</code>. I am passing an <code>DictionaryModel</code> Object. In the <code>DictionaryModel</code> object there is a Collection of <code>Words</code> and Another object called <code>SidePannelModel</code>. In the <code>SidePannelModel</code> there is another collection of <code>Words</code> and a string property. </p> <pre><code>DictionaryModel public class DictionaryModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public SidePannelModel SideModel { get; set; } public IEnumerable&lt;WordModel&gt; Language2 { get; set; } public void ToggleLanguage() { var temp = this.Language2; this.Language2 = this.SideModel.Language1; this.SideModel.Language1 = temp; OnPropertyChanged("DictionaryChanged"); } protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } SidePannelModel public class SidePannelModel { public IEnumerable&lt;WordModel&gt; Language1 { get; set; } public string SearchWord { get; set; } } </code></pre> <p>I am passing the DictionaryModel to DataContext in <code>MainWindow.xaml.cs</code>.</p> <pre><code> public partial class MainWindow : Window { private DictionaryModel dictionary; public MainWindow() { InitializeComponent(); dictionary = new DictionaryModel { SideModel = new SidePannelModel { SearchWord=null, Language1 = new List&lt;WordModel&gt;() { new WordModel() { Word = "Test1" } , new WordModel() { Word = "Test2" } , new WordModel() { Word = "Test3" } , new WordModel() { Word = "Test4" } , new WordModel() { Word = "Test5" } , new WordModel() { Word = "Test6" } , new WordModel() { Word = "Test7" } , new WordModel() { Word = "Test8" } , new WordModel() { Word = "Test9" } , new WordModel() { Word = "Test10" } } as IEnumerable&lt;WordModel&gt; }, Language2 = new List&lt;WordModel&gt;() { new WordModel() { Word = "Test1" } , new WordModel() { Word = "Test2" } , new WordModel() { Word = "Test3" } , new WordModel() { Word = "Test4" } , new WordModel() { Word = "Test5" } , new WordModel() { Word = "kkkkk" } , new WordModel() { Word = "Test7" } , new WordModel() { Word = "Test8" } , new WordModel() { Word = "Test9" } , new WordModel() { Word = "Test10" } } as IEnumerable&lt;WordModel&gt;, }; this.DataContext = dictionary; } } </code></pre> <p>This is how I pass the Collection of <code>Words</code> and the <code>SidePannelModel</code> to the user controllers in the <code>ManiWindow.xaml</code></p> <pre><code>&lt;Window x:Class="ThaiDictionary.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sidePanel="clr-namespace:ThaiDictionary" Title="MainWindow" Height="619"&gt; &lt;Window.Resources&gt; &lt;Style TargetType="{x:Type ToggleButton}" x:Key="toggleButtonStyle"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsChecked" Value="True"&gt; &lt;Setter Property="Content" Value="Thai to English" /&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsChecked" Value="False"&gt; &lt;Setter Property="Content" Value="English to Thai" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid Margin="0,0,2,0"&gt; &lt;DockPanel LastChildFill="True"&gt; &lt;Menu IsMainMenu="True" DockPanel.Dock="Top"&gt; &lt;MenuItem Header="_File" /&gt; &lt;MenuItem Header="_Edit" /&gt; &lt;MenuItem Header="_View" /&gt; &lt;MenuItem Header="_Window" /&gt; &lt;MenuItem Header="_Help" /&gt; &lt;/Menu&gt; &lt;StatusBar Height="22" DockPanel.Dock="Bottom"/&gt; &lt;sidePanel:SidePanel SideModel="{Binding SidePModel, Mode=TwoWay}" DockPanel.Dock="Left" MinWidth="200" MinHeight="540" Margin="5,5,5,1" Width="196"/&gt; &lt;DockPanel LastChildFill="True" DockPanel.Dock="Left"&gt; &lt;ToggleButton IsChecked="False" DockPanel.Dock="Top" HorizontalAlignment="Left" Height="30" Width="150" Style="{StaticResource toggleButtonStyle}" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked"&gt; &lt;/ToggleButton&gt; &lt;sidePanel:Description DockPanel.Dock="Top" WordsList2="{Binding Language2, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /&gt; &lt;/DockPanel&gt; &lt;/DockPanel&gt; &lt;/Grid&gt; </code></pre> <p></p> <p><code>SideModel</code> is defined in the <code>SidePannel.xaml.cs</code>. </p> <pre><code> public partial class SidePanel : UserControl { public static DependencyProperty SideModelProperty; static SidePanel() { SideModelProperty = DependencyProperty.Register("SideModel", typeof(SidePannelModel), typeof(SidePanel)); } public SidePanel() { InitializeComponent(); } public SidePannelModel SideModel { get { return (SidePannelModel)GetValue(SideModelProperty); } set { SetValue(SideModelProperty, value); } } } </code></pre> <p>But the <code>SidePanel</code> controller is not loaded with the <code>Word</code>s I am expecting. </p> <p>appropriate part of <code>SidePanel.xaml</code> is given below.</p> <pre><code>&lt;UserControl x:Class="ThaiDictionary.SidePanel" x:Name="SidePannelController" 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:l="clr-namespace:ThaiDictionary" mc:Ignorable="d" MinHeight="300" MinWidth="300" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Azure"&gt; &lt;UserControl.Resources&gt; &lt;/UserControl.Resources&gt; &lt;DockPanel LastChildFill="True" DataContext="{Binding SidePModel}"&gt; &lt;l:SearchTextBox SearchEventTimeDelay="00:00:02.00" Text="{Binding ElementName= SidePannelController, Path= SearchWord, Mode=TwoWay}" DockPanel.Dock="Top" Search="SearchTextBox_Search" HorizontalAlignment="Stretch" Background="Bisque"/&gt; &lt;ListBox Name="LeftSidePnel1" ItemsSource="{Binding ElementName= SidePannelController, Path= Language1, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemTemplate="{DynamicResource WordTemplate}" MinHeight="266" Height="auto" &gt; &lt;ListBox.Resources&gt; &lt;DataTemplate x:Key="WordTemplate"&gt; &lt;Label Content="{Binding Word}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.Resources&gt; &lt;/ListBox&gt; &lt;/DockPanel&gt; </code></pre> <p></p> <p>But when I run the project I can not see the words loaded in to the Sidepanel usercontrol. What is wrong with this code. I can not find a way out here. Can you give me some quick help.</p> <p>Edit:</p> <p>I have followed the naming conventions and changed the <code>SidePannel.xaml.cs</code>.</p> <p>Binding error:</p> <pre><code> System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=SidePanelController'. BindingExpression:Path=SearchWord; DataItem=null; target element is 'SearchTextBox' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=SidePanelController'. BindingExpression:Path=Language1; DataItem=null; target element is 'ListBox' (Name='LeftSidePnel1'); target property is 'ItemsSource' (type 'IEnumerable') </code></pre> <p>I have added the full SidePanel.xaml and I have changed the ElementName to SidePannelController as in the xaml file. But it still not getting loading the words</p> <p>New Edit:</p> <pre><code> System.Windows.Data Information: 41 : BindingExpression path error: 'SearchWord' 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=SideModel.SearchWord; DataItem='SidePanel' (Name='SidePannelController'); target element is 'SearchTextBox' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=SideModel.SearchWord; DataItem='SidePanel' (Name='SidePannelController'); target element is 'SearchTextBox' (Name=''); target property is 'Text' (type 'String') 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=SideModel.SearchWord; DataItem='SidePanel' (Name='SidePannelController'); target element is 'SearchTextBox' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=SideModel.SearchWord; DataItem='SidePanel' (Name='SidePannelController'); target element is 'SearchTextBox' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Information: 41 : BindingExpression path error: 'Language1' 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=SideModel.Language1; DataItem='SidePanel' (Name='SidePannelController'); target element is 'ListBox' (Name='LeftSidePnel1'); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=SideModel.Language1; DataItem='SidePanel' (Name='SidePannelController'); target element is 'ListBox' (Name='LeftSidePnel1'); target property is 'ItemsSource' (type 'IEnumerable') 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=SideModel.Language1; DataItem='SidePanel' (Name='SidePannelController'); target element is 'ListBox' (Name='LeftSidePnel1'); target property is 'ItemsSource' (type 'IEnumerable') </code></pre> <p>Thanks</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