Help with Binding tabcontrol that contains a treeview - silverlight

In a silverlight project i have a class:
class Foo{
List<Bar> Bars;
string BarName;
}
In my view model, I have:
List<Foo> Foos;
My TabControl is bound to Foos, and I'm using a Converter to convert my Foo class to a TabItem, with Header = BarName, and Content = Bars
My TabItem's content is just a TreeView, and I'd like to bind the TreeView's ItemSource to Bars
However I'm stuck trying to figure this out.

TabControl's ContentTemplate should be DataTemplate with TreeView and
<DataTemplate x:Key="ContentTemplate">
<sdk:TreeView ItemsSource={Binding}/>
</DataTemplate>
Update:
In code you can use template above:
yourTabItem.ContentTemplate = (DataTemplate)Application.Resources["ContentTemplate"];
Or without template:
yourTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding("Bars") { Source = yourSource });

Related

using WPF Caliburn, How do I change datagrid binding based on the combobox selection?

about to add the following features
If select this combobox, I want to change the itemssource of the datagrid.
Are there any examples related to this?
You can do the following:
Create a WPF project.
Create a view (xaml) with the combobox and datagrid inside it.
Create a view model for this newly created view and declare public properties (collection/list) for the ItemsSource of the combobox and the grid. Also have a property for the selected item of the combobox.
Set this view model as the data context of your view.
In the setter of the combobox's selected item - change the property which is bound to the datagrid's ItemsSource to the collection that you by calling a method or however you wish.
I did this:
Add the namespace for caliburn in xaml
xmlns:cal="http://www.caliburnproject.org"
Here is the combobox:
<ComboBox ItemsSource="{Binding ComboBoxItemSource}" SelectedItem="{Binding SelectedItem}" cal:Message.Attach="[Event SelectionChanged] = [ComboBoxSelectionChanged()]" />
and the viewmodel should be having this method:
public void ComboBoxSelectionChanged()
{
// here based on the SelectedItem you can change the ItemSource for the dataGrid.
}
Whenever you are changing the selectedItem of Combobox the method will get hit and based on the logic that you need you can assign the ItemSource for the dataGrid.
Hope this helps :)

trying to bind datagrid item source to a property in another class

I have a WPF app with a MainWindow. The MainWindow consists of several CLR properties of type ObservableCollection. The MainWindow has a datagrid, whose ItemsSource property is bound to one of the observable collections (works fine). Next, I have a dialog. Its purpose is to display one of the observable collections from the main window in a datagrid. The dialog gets instantiated in the MainWindow. Initially I was passing the ObservableCollection to the dialog's constructor, and copying it into the dialog's CLR property. Then I would set the DataContext of the dialog to itself, and bind the ItemsSource property in the datagrid to the name of the CLR property. This worked fine.
Is there a better way to do this instead of passing the observable collection through the constructor? I tried setting the ItemsSource property of the Datagrid in the dialog to the observable collection in the MainWindow by using the GUI editor, which generated a binding using RelativeAncestor, but the data did not show. The problem is I have a bunch of dialogs that are meant to display data from the MainWindow, and I feel like there should be a simpler solution rather than passing everything to dialog's constructor. Also, would the dialogs be considered SubViews? The main window is a view.
Let's say your Dialog control is named DialogControl and has a DependencyProperty named Items defined in its code behind. In the XAML, I would bind this property to the DataGrid like this:
<DataGrid ItemsSource="{Binding Items, RelativeSource={RelativeSource Mode=
FindAncestor, AncestorType={x:Type DialogControl}}" />
This RelativeSource binding will go off and search through the properties of your DialogControl class and find the Items property. Note: Do NOT set the DataContext of the UserControl to itself.
Now in your MainWindow.xaml.cs file where you instantiate your DialogControl, you can set the Items property:
DialogControl dialogControl = new DialogControl();
dialogControl.Items = someCollection;
dialogControl.Show();
UPDATE >>>
Oh I see what you're after now... you want to bind from your UserControl to the actual collection in the MainWindow.xaml.cs file. You can still follow my advice, but instead of having the DependencyProperty in your DialogControl, you need to have it in your MainWindow.xaml.cs file. In that case, your binding in the UserControl would be:
<DataGrid ItemsSource="{Binding Items, RelativeSource={RelativeSource Mode=
FindAncestor, AncestorType={x:Type MainWindow}}" />
For this to work, the Items property must be a DependencyProperty.

How to bind a string collection to TreeViewItem

In my WPF application, I have added a TreeView. I have a Collection which I want to bind to the parent nodes(TreeViewItems) of the TreeView. Here is my code for doing this.
<TreeView Name="treeView" Width="200">
<TreeViewItem Header="{Binding ElementryNames}"/>
</TreeView>
The above code displays a node in the treeView which only displays "Collection" as the text of the TreeView node. What Im doing wrong to get this done?
Thanks
Basically you need to specify the Itemssource for your treeview. Then you can define via template for each item from your list how it should be displayed.
With your currently XAML the collection itself is interpreted as item and the default binding for elements is basically the ToString() method.
MSDN article
There should be an item source defined for the treeview. This observable collection can be of a type class. Inside this class you can define a property "ElementryNames" of type string.
You need to bind ItemSource to the TreeView and then apply Template for every item.

Binding a combobox in XAML to a childwindow property

I want to display a child window that contains a combobox with several values coming from one of the child window's property:
public partial class MyChildWindow : ChildWindow
{
private ObservableCollection<MyClass> _collectionToBind = // initialize and add items to collection to make sure it s not empty...
public ObservableCollection<MyClass> CollectionToBind
{
get { return _collectionToBind; }
set { _collectionToBind = value; }
}
}
How do I bind in XAML my combobox to the ComboBoxContent collection (both are in the same class)? I've tried several things such as:
<ComboBox x:Name="linkCombo" ItemsSource="{Binding Path=CollectionToBind }" DisplayMemberPath="Description">
I've only been able to bind it in the code behind file and would like to learn the XAML way to do it.
Thank you!
In this case I would use ElementToElement binding like this:-
<ComboBox x:Name="linkCombo" ItemsSource="{Binding Path=Parent.CollectionToBind, ElementName=LayoutRoot }" DisplayMemberPath="Description">
You give the Content element of the ChildWindow the x:Name of LayoutRoot (in the standard template for child window this is done for you). Hence you can bind to this named element and navigate to the containing ChildWindow by using its Parent property.
Using DataContext = this is tempting and works in simple scenarios but things can get awkward in more complex requirements when the DataContext has already been taken in this way.
You need to set the DataContext of the ChildWindow to what contains the values you'd like to bind to. In this case where you're putting the values you want to bind to on the ChildWindow itself so just put a line in the constructor assigned the DataContext to itself.
DataContext = this;
You can also do this using a RelativeSource binding in the XAML, like this:
{Binding Path=CollectionToBind, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}
However, a better way to do this would be to put the CollectionToBind in a separate class and assign it to the Window DataContext. Now both the Window and the XAML Bindings can all refer to the same class as the DataContext and you can isolate more of your logic into this class rather than putting it in the Window implementation.

WPF Dynamically Setting CellTemplate and Binding

I have a GridView control and I am adding GridViewColumns. I was using DisplayMemberBinding property of GridViewColumn but now I want to use the CellTemplate. I am binding to a dictionary.
The following code worked with DisplayMemberBinding:
var column = new GridViewColumn
{
Header = current.Key,
DisplayMemberBinding = new Binding("[" + current.Key + ]")
};
Now, I need to do the same with CellTemplate but for some reason I am not sure why it is not displaying the items.
var column = new GridViewColumn
{
Header = current.Key,
CellTemplate = (DataTemplate)FindResource("GridViewTextBlockDataTemplate"),
};
And here is the DataTemplate defined in Window.Resources:
<DataTemplate x:Key="GridViewTextBlockDataTemplate" x:Name="GridViewTextBlockDataTemplate">
<TextBlock Text="{Binding Path=[Key]}"></TextBlock>
</DataTemplate>
Thanks,
Azam
Your solution is correct except that to get the value in the textblock you just need to have Keyword Binding and no need to provide path as you are binding the datatemplate, it automatically assigns the value.
If that does not work try to use datatemplate selector for the datagrid and For a detailed explanation on datatemplate selector please have a look a these link:
WPF DataTemplate Selector Tutorial

Resources