I am trying to bind to a TreeView with ReactiveUI in WPF, but nothing is being displayed. Can anyone tell me where I am going wrong, or give me some advice on how to debug it?
Binding in View constructor:
this.OneWayBind(ViewModel,
vm => vm.NonTableItemCommentsViewModels,
view => view.CommentTree.ItemsSource);
Xaml:
<TreeView Name="CommentTree" ItemsSource="{Binding}">
<TreeView.Resources>
<!-- Template for Items -->
<HierarchicalDataTemplate DataType="{x:Type viewModels:NonTableItemCommentsViewModel}"
ItemsSource="{Binding Path=Comments, Mode=OneWay}">
<TextBlock Text="{Binding Path=ItemId, Mode=OneWay}" />
</HierarchicalDataTemplate>
<!-- Template for Comments -->
<DataTemplate DataType="{x:Type viewModels:CommentViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Text, Mode=OneWay}"
Margin="3,0" />
<TextBlock Text="{Binding Path=Author, Mode=OneWay}"
Margin="3,0" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Items and Comments are both ObservableCollections.
Related
I have a user control defines in my Application.Resources and want to use it as a DataTemplate for a ComboBox.
The user control:
<TextBlock
x:Key="ListItemView"
Text="{Binding Name}"
ToolTip="{Binding ToolTip}"/>
The Combox Box in my Window:
<ComboBox
ItemsSource="{Binding ComboBoxItems}"
SelectedItem="{Binding SelectedComboBoxItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- TODO how to use StaticResource ListItemView in here? -->
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You must put your user control into DataTemplate
<DataTemplate x:Key="ListItemView">
<TextBlock Text="{Binding Name}"
ToolTip="{Binding ToolTip}"/>
</DataTemplate>
Use your data template
<ComboBox ItemsSource="{Binding ComboBoxItems}"
SelectedItem="{Binding SelectedComboBoxItem}"
ItemTemplate="{StaticResource ListItemView}">
</ComboBox>
I am beginner on WPF and need help.
Problem:
I have 2 Tree View on the form and need to apply 2 different style on pair of 2 Tree View Comparison.
Is there any way we can achieve this ?
I have 2 Tree View look like this
<TreeView x:Name="Source1PermissionTreeView" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="630" Width="890" >
<TreeView.Resources>
<DataTemplate DataType="{x:Type entities:SharePointUser}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\user.png"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</DataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SitePermission}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\sitepermission.png"/>
<TextBlock Text="{Binding Path=Title}"></TextBlock>
</DockPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SharePointGroup}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\group.png"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SharePointList}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\List.PNG"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SharePointSite}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\Site.png"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
How can I apply style in tree view?
Is there any possible solution to show custom ContextMenu on specific TreeViewItem?
My TreeView looks like that:
<TreeView Name="AliasTree" ItemsSource="{Binding aliases}">
<TreeView.Resources >
<HierarchicalDataTemplate DataType="{x:Type models:ConnectionModel}" ItemsSource="{Binding schemas}">
<TextBlock Text="{Binding alias}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:SchemaModel}" ItemsSource="{Binding schema_collections}">
<TextBlock Text="{Binding SCHEMA_NAME}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:SchemaCollection}" ItemsSource="{Binding collection}">
<TextBlock Text="{Binding SCHEMA_COLLECTION_NAME}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type models:TableModel}" >
<TextBlock Text="{Binding TABLE_NAME}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
I want to show ContextMenu only on TreeViewItem based on "TableModel".
Later I will make custom ContextMenu for other elements.
How can I do that?
Thank You for any help,
Wiktor
all night racking my head - I can not figure out how to bind a selected segment TreeView variable Mvvm
<TreeView ItemsSource="{Binding Subunits}">
<!-- Шаблон подразделения -->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<TextBlock Text="{Binding SubunitName}"/>
<!-- Шаблон должности -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type local:Employee}">
<TextBlock Text="{Binding PositionName}"/>
<!-- Шаблон сотрудника -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0" MouseDown="qwe"/>
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Patronymic}" Margin="0,0,5,0"/>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
How can i do it?
sorry for my english :(
I am pretty new in the usage of the DataBinding concept in Windows Phone. I am trying to bind a Pivot page with a list of items, as well as a ListBox in each PivotItem with another list (of criterias). I have a Page with the following XAML:
<phone:Pivot Name="ItemsPivot" Title="MY APPLICATION">
<phone:Pivot.ItemTemplate>
<DataTemplate>
<StackPanel Margin="12,17,0,28">
<ListBox Name="CriteriasListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<TextBlock Text="{Binding Name}"/>
<tk:Rating />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
I then try to specify the ItemSource of each binded control in the C# code like this:
...
ItemsPivot.ItemsSource = ItemList;
CriteriasListBox.ItemsSource = CriteriaList; // <-- CriteriasListBox not accessible !!
...
But there is an error stating that "the name 'CriteriasListBox' does not exist in the current context"... As I mention, I am pretty new with this technology.
Can I have some advice, solution or resources on how do make this nested binding work?
Thank you !
Do like this,
<UserControl.Resources>
<DataTemplate x:Key="MyPivotItemTemplate">
<controls:PivotItem Header="first" >
<ListBox x:Name="CriteriasListBox" Margin="0,0,12,0" ItemsSource="{Binding CriteriaList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
</DataTemplate>
</UserControl.Resources>
Then in your pivot
<Pivot ItemsTemplate="{StaticResource MyPivotItemTemplate}" Items="{Binding ItemList}"
One of the ways if you have different criteria list for different items is to add criteria list property to your PivotItemViewModel. Your c# code will look like this
ItemsPivot.ItemsSource = ItemList;
where every item in ItemList contains CriteriaList. Your xaml code will look like
<phone:Pivot Name="ItemsPivot" Title="MY APPLICATION">
<phone:Pivot.ItemTemplate>
<DataTemplate>
<StackPanel Margin="12,17,0,28">
<TextBlock Name="PivotTextBlock" Text="{Binding Name}"/>
<ListBox ItemsSource="{Binding Criterions}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<TextBlock Name="ListTextBlock" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
Binding for PivotTextBlock retrieves Name property from the PivotItemViewModel. Binding for ListTextBlock retrieves Name property from Criteria.
If criteria list the only in your application here is a good start
Here is another good way I found...
http://www.codeproject.com/Articles/47229/A-LINQ-Tutorial-WPF-Data-Binding-with-LINQ-to-SQL
Here is a code snippet:
<DataTemplate DataType="{x:Type LINQDemo:Category}">
<Border Name="border" BorderBrush="ForestGreen"
BorderThickness="1" Padding="5" Margin="5">
<StackPanel>
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold" FontSize="14"/>
<ListView ItemsSource="{Binding Path=Books}"
HorizontalContentAlignment="Stretch" BorderThickness="0" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Click="LoadIndividualBook"
CommandParameter="{Binding}"
ToolTip="Display book details">
<TextBlock Text="{Binding Path=Title}"/></Hyperlink>
...