Data binding cardview control wpf - wpf

Here is my xaml
<syncfusion:CardView Grid.Column="1"
Grid.Row="2"
Grid.ColumnSpan="4"
Grid.RowSpan="4"
ItemsSource="{Binding Events}">
<syncfusion:CardView.DataContext>
<viewModel:DefaultViewModel />
</syncfusion:CardView.DataContext>
<syncfusion:CardView.ItemTemplate>
<DataTemplate>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBoxItem>
<ListBoxItem.DataContext>
<viewModel:DefaultViewModel />
</ListBoxItem.DataContext>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Event Name:" />
<TextBlock Text="{Binding EventName}"
Margin="5,0,0,0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Event Genre:" />
<TextBlock Text="{Binding EventGenre}"
Margin="5,0,0,0" />
</StackPanel>
</ListBoxItem>
</ListBox>
</DataTemplate>
</syncfusion:CardView.ItemTemplate>
<syncfusion:CardView.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding EventName}" />
</DataTemplate>
</syncfusion:CardView.HeaderTemplate>
</syncfusion:CardView>
My viewModel
public DefaultViewModel()
{
CustomerDatabaseEntities context = new CustomerDatabaseEntities();
Events = (from data in context.Event_Details select data.eventTitle).ToList();
}
public string EventName {get;set;}
public string EventGenre {get;set;}
My Model
public partial class Event_Details
{
public string eventTitle { get; set; }
public string eventGenre { get; set; }
}
I'm trying to display each value of eventTitle and eventGenre in its own card similar to this:
.
Each event will have its own card, with its respective details.Although the card view was able to show that i had two different events(refer to link above), both the eventTitle and eventGenre were blank. How would i be able to display each of these values in their own cards?

<DataTemplate>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Event Name:" />
<TextBlock Text="{Binding eventTitle }"
Margin="5,0,0,0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Event Genre:" />
<TextBlock Text="{Binding eventGenre }"
Margin="5,0,0,0" />
</StackPanel>
</ListBoxItem>
</ListBox>
</DataTemplate>

Related

Databind to a ListView or TreeView with an Observable Collection with several array properties

I can't seem to get the output to display the way I want. I'm pretty new to XAML. Currently my out put looks like:
enter image description here
If you look at the screenshot, the output is almost correct. The 1st two columns are properties of my LiveCameraResult object that are added to my observable collect. The next four columns (FaceId, Age, Gender, Emotion) also come from a property of the LiveCameraResult object but the property is an array (Faces) and I want ouput of ALL the Faces in this object to display. So far the only way I have been able to get output to display at all for those four columns is to only show the 1st entity in the array by indexing to [0]. How can I fix my output to show multiple faces data. Here's what my LiveCameraResult Class looks like:
public class LiveCameraResult
{
public DateTime TimeStamp { get; set; }
public string SelectedCamera { get; set; } = null;
public string TopEmotion { get; set; } = null;
public string FirstIdentity { get; set; }
public List<String> Identity = new List<string>();
public Microsoft.ProjectOxford.Face.Contract.Face[] Faces { get; set; } = null;
public Microsoft.ProjectOxford.Common.Contract.EmotionScores[] EmotionScores { get; set; } = null;
public string[] CelebrityNames { get; set; } = null;
public Microsoft.ProjectOxford.Vision.Contract.Tag[] Tags { get; set; } = null;
}
I tried the following XAML code with no luck:
<Grid Grid.ColumnSpan="2" Margin="5,0,0,-74.173" Grid.Row="2">
<DockPanel x:Name="scrollViewContent" Margin="0,10,-5,-16.571" Height="154.901" VerticalAlignment="Top" >
<!--<ListView Name="LogView" ItemsSource="{Binding}" Background="#FFD4C9C9" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="0,0,5,0" MinHeight="153.143" DockPanel.Dock="Top" IsSynchronizedWithCurrentItem="True" >-->
<!--<ListView.View >-->
<TreeView ItemsSource="{Binding}">
   
<!-- Conference template -->
   
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding}">
<TextBlock Foreground="Red" Text="{Binding TimeStamp}" />
<!-- Team template -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Foreground="Red" Text="{Binding Path=Faces.FaceAttributes.Gender}"/>
<!-- Player template -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
My original XAML looked like this:
<Grid Grid.ColumnSpan="2" Margin="5,0,0,-74.173" Grid.Row="2">
<DockPanel x:Name="scrollViewContent" Margin="0,10,-5,-16.571" Height="154.901" VerticalAlignment="Top" >
<ListView Name="LogView" ItemsSource="{Binding}" Background="#FFD4C9C9" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="0,0,5,0" MinHeight="153.143" DockPanel.Dock="Top" IsSynchronizedWithCurrentItem="True" >
<ListView.View >
<GridView ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" >
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Timestamp" Content="Timestamp" Padding="10,0,2,0" >
<GridViewColumnHeader.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="White" Offset="0.4091"/>
<GradientStop Color="#FFF7F8F9" Offset="1"/>
</LinearGradientBrush>
</GridViewColumnHeader.Background>
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding TimeStamp}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Source" Content="Source" Padding="10,0,2,0" ScrollViewer.CanContentScroll="True" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding SelectedCamera}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Face ID" Content="Face ID" Padding="10,0,2,0" MinWidth="159.413" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock DataContext="{Binding Path=Faces}" Text="{Binding Path=FaceId}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Gender" Content="Gender" Padding="10,0,2,0" MinWidth="61.102" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Faces[0].FaceAttributes.Gender}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Age" Content="Age" Padding="10,0,2,0" MinWidth="42.797" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Faces[0].FaceAttributes.Age}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Emotion" Content="Emotion" Padding="10,0,2,0" MinWidth="110" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=TopEmotion}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="auto" >
<GridViewColumnHeader Tag="Identity" Content="Identity" Padding="10,0,2,0" MinWidth="119.388" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=FirstIdentity}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Grid>
Here's just one LiveCameraResult object in json format:
{"TimeStamp":"2017-09-30T17:34:26.3317547-04:00","SelectedCamera":"Camera 1","TopEmotion":null,"Faces":[{"FaceId":"9b512175-59f3-4a1c-b19e-d650cda0bffc","FaceRectangle":{"Width":159,"Height":159,"Left":311,"Top":233},"FaceLandmarks":null,"FaceAttributes":{"Age":48.4,"Gender":"male","HeadPose":{"Roll":1.9,"Yaw":7.6,"Pitch":0.0},"Smile":0.0,"FacialHair":{"Moustache":0.5,"Beard":0.5,"Sideburns":0.3},"Emotion":{"Anger":0.0,"Contempt":0.0,"Disgust":0.0,"Fear":0.0,"Happiness":0.0,"Neutral":0.997,"Sadness":0.003,"Surprise":0.0},"Glasses":"NoGlasses","Blur":null,"Exposure":null,"Noise":null,"Makeup":{"EyeMakeup":false,"LipMakeup":false},"Accessories":null,"Occlusion":null,"Hair":{"Bald":0.17,"Invisible":false,"HairColor":[{"Color":"Black","Confidence":0.99},{"Color":"Other","Confidence":0.64},{"Color":"Gray","Confidence":0.59},{"Color":"Brown","Confidence":0.56},{"Color":"Red","Confidence":0.09},{"Color":"Blond","Confidence":0.04}]}}},{"FaceId":"19f12175-59f3-4a1c-b19e-83cccda06aa2","FaceRectangle":{"Width":120,"Height":120,"Left":211,"Top":133},"FaceLandmarks":null,"FaceAttributes":{"Age":36.4,"Gender":"female","HeadPose":{"Roll":1.9,"Yaw":7.6,"Pitch":0.0},"Smile":0.0,"FacialHair":{"Moustache":0.0,"Beard":0.0,"Sideburns":0.3},"Emotion":{"Anger":1.0,"Contempt":0.0,"Disgust":0.0,"Fear":0.0,"Happiness":0.0,"Neutral":0.597,"Sadness":0.003,"Surprise":0.0},"Glasses":"SunGlasses","Blur":null,"Exposure":null,"Noise":null,"Makeup":{"EyeMakeup":true,"LipMakeup":false},"Accessories":null,"Occlusion":null,"Hair":{"Bald":0.17,"Invisible":false,"HairColor":[{"Color":"Black","Confidence":0.99},{"Color":"Other","Confidence":0.64},{"Color":"Gray","Confidence":0.59},{"Color":"Brown","Confidence":0.56},{"Color":"Red","Confidence":0.09},{"Color":"Blond","Confidence":0.04}]}}}],,"EmotionScores":null,"CelebrityNames":null,"Tags":[{"Name":"person","Confidence":0.99842345714569092,"Hint":null},{"Name":"man","Confidence":0.981597900390625,"Hint":null},{"Name":"indoor","Confidence":0.95850932598114014,"Hint":null},{"Name":"window","Confidence":0.9486764669418335,"Hint":null}],"Animal":null,"Building":null,"Trans":null,"People":null,"Object":null,"Food":null,"Text":null,"Plant":null,"Indoor":null,"Dark":null,"Sky":null,"Outdoor":null,"Abstract":null,"Ocr":null}
To summarize, This log updates every three seconds and give real time data. The TimeStamp, and Camera output is right but the rest of the columns come from properties that are arrays inside an object that hold data for multiple faces. How can I display data so that if at say 10:30 from camera 1 it picks up 4 faces I can see their face IDs, gender, age and identities.
I've tried using a Listbox and a Datatemplate, check it out:
<StackPanel HorizontalAlignment="Left">
<StackPanel.ScrollOwner>
<ScrollViewer/>
</StackPanel.ScrollOwner>
<StackPanel Orientation="Horizontal" Background="White">
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="TimeStampTextBLK" TextWrapping="Wrap" Text="Time Stamp" FontSize="24" Padding="50,10"/>
</Border>
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="SourceTextBLK" TextWrapping="Wrap" Text="Source" FontSize="24" Padding="20,10"/>
</Border>
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="FaceIDTextBLK" TextWrapping="Wrap" Text="Face ID" FontSize="24" Padding="150,10"/>
</Border>
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="GenderTextBLK" TextWrapping="Wrap" Text="Gender" FontSize="24" Padding="30,10"/>
</Border>
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="AgeTextBLK" TextWrapping="Wrap" Text="Age" FontSize="24" Padding="30,10"/>
</Border>
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="EmotionTextBLK" TextWrapping="Wrap" Text="Emotion" FontSize="24" Padding="60,10"/>
</Border>
<Border BorderThickness="2" BorderBrush="#FFCB9C9C">
<TextBlock x:Name="IdentityTextBLK" TextWrapping="Wrap" Text="Identity" FontSize="24" Padding="60,10"/>
</Border>
</StackPanel>
<ListBox x:Name="MainListBox" Background="#FFCB9C9C" BorderBrush="{x:Null}" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="{Binding ActualWidth, ElementName=TimeStampTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=TimeStamp}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
<Border Width="{Binding ActualWidth, ElementName=SourceTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=SelectedCamera}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
<Border Width="{Binding ActualWidth, ElementName=FaceIDTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=Faces}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
<Border Width="{Binding ActualWidth, ElementName=GenderTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=Faces[0].FaceAttributes.Gender}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
<Border Width="{Binding ActualWidth, ElementName=AgeTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=Path=Faces[0].FaceAttributes.Age}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
<Border Width="{Binding ActualWidth, ElementName=EmotionTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=TopEmotion}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
<Border Width="{Binding ActualWidth, ElementName=IdentityTextBLK, Mode=OneWay}">
<TextBlock Text="{Binding Path=FirstIdentity}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Margin="5"/>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>

Delete a subitem in a tree view with heirarchical data template

Please find below my XAML,
<loc:MultiSelectTreeView Height="295" ScrollViewer.VerticalScrollBarVisibility="Visible" BorderThickness="1" Background="WhiteSmoke" x:Name="GridListEmulation" Grid.Row="2" BorderBrush="Gray" ItemsSource="{Binding EmulationCollection,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Margin="0,2,0,-2"
ItemContainerStyle="{StaticResource MultiSelectTreeViewItemStyle}" SelectedItemChanged="GridListEmulation_SelectedItemChanged">
<TreeView.ItemTemplate >
<HierarchicalDataTemplate ItemsSource="{Binding Items,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Stream" Width="60"/>
<ColumnDefinition SharedSizeGroup="Port" Width="55"/>
<ColumnDefinition SharedSizeGroup="Device Name" Width="100"/>
<ColumnDefinition SharedSizeGroup="Count" Width="50"/>
<ColumnDefinition SharedSizeGroup="FromMAC" Width="120"/>
<ColumnDefinition SharedSizeGroup="State" Width="60"/>
<ColumnDefinition SharedSizeGroup="MACAddress" Width="120"/>
<ColumnDefinition SharedSizeGroup="EmulationIPv4Address" Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding StreamId}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="1" Text="{Binding Port}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="2" Text="{Binding EmulationDeviceName}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="3" Text="{Binding SessionCount}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="4" Text="{Binding SourceMAC}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="5" Text="{Binding State}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="6" Text="{Binding SimulatedMAC}" Style="{StaticResource TextBlockStyle}"/>
<TextBlock Grid.Column="7" Text="{Binding EmulationIPv4Address}" Style="{StaticResource TextBlockStyle}"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</loc:MultiSelectTreeView>
I am trying to delete a subitem in this multi treeview which is bound to my observable collection as shown below,
tvm.EmulationCollection.RemoveAt(GridListEmulation.Items.IndexOf(subItem));
But i always get the index as -1 for subitem and then gives an exception. Please let me know if any way to get the subitem in the tree view item of the heirarchical data template and delete it? Thanks in advance.
here is a basic example of using a VM to host a tree
public class TreeVM : BindableBase
{
public TreeVM()
{
AddChild = new DelegateCommand(() => Items.Add(new TreeVM() {Parent = this }));
RemoveMe = new DelegateCommand(() => Parent.Items.Remove(this));
}
private string _Text;
public string Text
{
get { return _Text; }
set { SetProperty(ref _Text, value); }
}
private TreeVM _Parent;
public TreeVM Parent
{
get { return _Parent; }
set { SetProperty(ref _Parent, value); }
}
public ObservableCollection<TreeVM> Items { get; } = new ObservableCollection<TreeVM>();
public DelegateCommand AddChild { get; set; }
public DelegateCommand RemoveMe { get; set; }
}
then hosted on this XAML
<StackPanel>
<Button Content="Add" Command="{Binding AddChild}"/>
<TreeView ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Text}"/>
<Button Content="Add" Command="{Binding AddChild}"/>
<Button Content="Delete" Command="{Binding RemoveMe}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
as you can see the child is responsible for removing itself from the tree, and this works because your VM knows its parent as well as its children

WPF different dataContext for comboBox ItemsSource

I have a ItemControl and inside I have a comboBox, what I'm trying to achieve is to have a different dataContext for the comboBox itemsSource
This is my itemControl:
<ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel Margin="8">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
<StackPanel >
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Name:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Name}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Type:" FontSize="15" FontWeight="SemiBold"/>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.EmployeeStatus}"
SelectedValue="{Binding Type}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Units:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Units}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Range:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Range}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Scale:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Scale}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Reason:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Reason}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Description:" FontSize="15" FontWeight="SemiBold" />
<TextBox Text="{Binding Description}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
</StackPanel>
</Border>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I saw something similar but couldn't make the connection to my problem
WPF ComboBox bind itemssource to different datacontext in MVVM
Solution 1: Each ComboBox has different items
You must create appropriate type with ComboBox items source.
Example:
public class MyType
{
public string Name { get; set; }
public List<string> Types { get; set; }
public string Type { get; set; }
//...
}
Now you can binding ComboBox items source to different source:
<ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel Margin="8">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
<StackPanel >
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Name:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Name}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Type:" FontSize="15" FontWeight="SemiBold"/>
<ComboBox ItemsSource="{Binding Path=Types}"
SelectedValue="{Binding Type}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
...
</ItemsControl>
Here you can find example solution.
Solution 2: All ComboBox have the same items
public class MyType
{
public string Name { get; set; }
public string Type { get; set; }
}
class MyViewModel
{
private List<MyType> _types;
public List<MyType> Types
{
get { return _types; }
set { _types = value; }
}
public List<string> TypesItemsSource { get; set; }
}
XAML code:
<ItemsControl ItemsSource="{Binding Types}" Margin="0,25,0,0" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel Margin="8">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
<StackPanel >
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Name:" FontSize="15" FontWeight="SemiBold"/>
<TextBox Text="{Binding Name}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Type:" FontSize="15" FontWeight="SemiBold"/>
<ComboBox ItemsSource="{Binding Path=DataContext.TypesItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
SelectedValue="{Binding Type}" Width="70" Margin="10,0,0,0"/>
</StackPanel>
...
</ItemsControl>
Here you can find example solution.
i use "marker interfaces" to find the right datacontext
public interface IDataContextMarker{} //just an empty interface
so within the view where your viewmodel is bound to implement this interface
public class MyView : IDataContextMarker {}
and now you can use the interface with relativesource binding instead of controls or ancestorlevel, that makes thing easier for some cases :)
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:IDataContextMarker}}, Path=DataContext.EmployeeStatus}"

Databinding Class to Silverlight controls tooltip

Having a Author Class below
Public Class Author
{
Public String Name{get;set;}
Public String Description{get;set;}
Public int NumberOfBooks{get;set;}
}
and databinding List<Author> to Listbox through DataTemplate like below
<ListBox ItemsSource="{Binding Authors}" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel ToolTipService.ToolTip="{Binding Description}" ToolTipService.Placement="Right">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How do I generate a tooltip using Xaml descriptive declarative binding like below
AuthorName ( NumberOfBooks)
Description
You can go simple.
<TextBlock Text="{Binding Name}">
<ToolTipService.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock>
<Run Text="{Binding AuthorName}"/>
<Run Text=" "/>
<Run Text="{Binding NumberOfBooks}"/>
</TextBlock>
<TextBlock MaxWidth="150"
Text="{Binding Description}" TextWrapping="Wrap"/>
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
Or get fancy.
<TextBlock Text="{Binding Name}">
<ToolTipService.ToolTip>
<ToolTip>
<StackPanel>
<Border Background="CadetBlue" Margin="5" Padding="5">
<TextBlock Text="{Binding AuthorName}" HorizontalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding NumberOfBooks, StringFormat='No. of Books: \{0\}'}"/>
<TextBlock MaxWidth="150"
Text="{Binding Description}" TextWrapping="Wrap"/>
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
Hope this helps :)

Dispalying pools depending od parameter in wpf treeview

I've got a wpf treeview control and depending on parameter in constructor, I want to display pool NumberOfHotels or not display.
<Grid>
<StackPanel Name="stackPanel1">
<GroupBox Header="Wybierz"
Height="354"
Name="groupBox1"
Width="Auto">
<TreeView Name="structureTree"
SelectedItemChanged="structureTree_SelectedItemChanged"
Grid.Row="0" Grid.Column="0"
ItemsSource="{Binding}"
Height="334" Width="Auto"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
PreviewMouseRightButtonUp="structureTree_PreviewMouseRightButtonUp"
FontFamily="Verdana" FontSize="12"
BorderThickness="1" MinHeight="0"
Padding="1" Margin="-1"
Cursor="Hand">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type MyService:Country}"
ItemsSource="{Binding Path=ListOfRegions}">
<StackPanel Orientation="Horizontal">
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text="{Binding Path=Name}"/>
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text=" "/>
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text="H:"/>
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text="{Binding Path=NumberOfHotels}"/>
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text=" "/>
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text=" S:"/>
<TextBlock TextAlignment="Justify"
VerticalAlignment="Center"
Text="{Binding Path=NumberOfZones}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</GroupBox>
</StackPanel>
</Grid>
Is there any way to do this ?
Yes. In your constructor set a property of type Visibility like this:
public class MyUserControl : UserControl
{
public TreeViewVisibility { get; private set; }
public MyUserControl(bool showTreeView)
{
TreeViewVisibility = showTreeView ? Visibility.Visible : Visibility.Collapsed;
...
}
}
And bind to it in your XAML:
...
<TreeView Visibility="{Binding TreeViewVisibility,
RelativeSource={RelativeSource FindAncestor,local:MyUserControl,1}}" />

Resources