How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

The answer is this: <DataTemplate x:Key=”CodeGenerationMenuTemplate”> <MenuItem Header=”{Binding Title}” Command=”{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}” CommandParameter=”{Binding Title}”/> </DataTemplate> I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.

Bind Collection to StackPanel

Alright I have figured it out… Using an ItemsControl solved the problem… <ItemsControl x:Name=”tStack” ItemsSource=”{Binding Items}”> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation=”Horizontal”/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Button Content=”{Binding ItemName}”/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

Difference between Observer, Pub/Sub, and Data Binding

There are two major differences between Observer/Observable and Publisher/Subscriber patterns: Observer/Observable pattern is mostly implemented in a synchronous way, i.e. the observable calls the appropriate method of all its observers when some event occurs. The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue). In the Observer/Observable pattern, the observers are aware … Read more

Best way for automatic databinding between database and user interface in java swing app?

This is a complex topic that might make a good Community Wiki. I’ve only scratched the surface, but NetBeans has an evolving capability in this area. It should be on your short list. See these help topics & links: Generating JPA Controller Classes from Entity Classes Binding Data to a Swing Component Java Persistence Tasks: … Read more

Coerce a WPF TextBox not working anymore in .NET 4.0

Try using in xaml Explicit instead of PropertyChanged: <TextBox Text=”{Binding Percentage, Mode=TwoWay, UpdateSourceTrigger=Explicit, TargetNullValue={x:Static System:String.Empty}}” TextChanged=”TextBox_TextChanged” /> and in code behind UpdateSource instead of UpdateTarget private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { // Removed safe casts and null checks ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource(); } Tested it and it works. Btw this problem will probably be resolved in a … Read more

CompositeCollection + CollectionContainer: Bind CollectionContainer.Collection to property of ViewModel that is used as DataTemplates DataType

Due to the issue with data binding on CollectionContainer as described http://social.msdn.microsoft.com/Forums/vstudio/en-US/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a/collectioncontainer-does-not-support-relativesource?forum=wpf I now use the following approach: <ListBox> <ListBox.Resources> <CollectionViewSource x:Key=”DogCollection” Source=”{Binding Dogs}”/> <CollectionViewSource x:Key=”CatCollection” Source=”{Binding Cats}”/> </ListBox.Resources> <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection=”{Binding Source={StaticResource DogCollection}}”/> <CollectionContainer Collection=”{Binding Source={StaticResource CatCollection}}”/> </CompositeCollection> </ListBox.ItemsSource> <!– … –> </ListBox> Edit: The CompositeCollection class does not derive from FrameworkElement and … Read more

How to bind Dictionary to ListBox in WinForms

var choices = new Dictionary<string, string>(); choices[“A”] = “Arthur”; choices[“F”] = “Ford”; choices[“T”] = “Trillian”; choices[“Z”] = “Zaphod”; listBox1.DataSource = new BindingSource(choices, null); listBox1.DisplayMember = “Value”; listBox1.ValueMember = “Key”; (Shamelessly lifted from my own blog: Bind a ComboBox to a generic Dictionary.) This means you can use SelectedValue to get hold of the corresponding dictionary … Read more