Dispalying pools depending od parameter in wpf treeview - wpf

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}}" />

Related

Data binding cardview control 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>

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}"

WPF Nested Databinding

I'm trying to bind data into nested StackPanels in WPF using the following code - to no avail. Can anybody spot where I'm going wrong?
<Page.Resources>
<DataTemplate x:Key="MinuteTimeSlotTemplate">
<TextBlock Text="{Binding Path=TourName}" Background="Blue" />
</DataTemplate>
<DataTemplate x:Key="HourlyTimeSlotTemplate">
<StackPanel>
<Border Height="120" BorderThickness="0,0,0,1" BorderBrush="#DDD" x:Name="parentRow">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="123"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="0,0,10,0" MouseUp="StackPanel_MouseUp_1">
<TextBlock Foreground="#4585B9" FontWeight="Bold" FontFamily="Trebuchet MS" FontSize="20" HorizontalAlignment="Right">
<Run Text="{Binding Path=Time}"/></TextBlock>
<TextBlock FontFamily="Trebuchet MS" Foreground="#808080" HorizontalAlignment="Right"><Run Text="View Bookings"/></TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1" Margin="10,0,10,0" x:Name="stk">
<ItemsControl ItemTemplate="{StaticResource MinuteTimeSlotTemplate}" ItemsSource="{Binding Path=HourlySlots}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</Grid>
</Border>
</StackPanel>
</DataTemplate>
</Page.Resources>
<helpers:AnimatedScrollViewer x:Name="scrlHours" Grid.Column="1" PanningMode="Both" PanningDeceleration="5" PanningRatio="2">
<ItemsControl x:Name="TimeSlotList" ItemTemplate="{StaticResource HourlyTimeSlotTemplate}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</helpers:AnimatedScrollViewer>
I set the parent StackPanels ItemsSource in code:
public HourlyCalendar() {
InitializeComponent();
TimeSlotList.ItemsSource = new Models.TimeSlots(DateTime.Today).HourlySlots;
}
And here's my model:
public class TimeSlots {
public TimeSlots(DateTime? day) {
this.HourlySlots = DataManager.GetCalendarDayTimeSlots(day);
}
public IEnumerable<TimeSlot> HourlySlots { get; set; }
}
The parent StackPanel binds as expected, but I cannot figure out how to bind the child StackPanel...
Turns out it was rather simple:
<helpers:AnimatedScrollViewer x:Name="scrlHours" Grid.Column="1" PanningMode="Both" PanningDeceleration="5" PanningRatio="2">
<ItemsControl x:Name="TimeSlots">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Height="120" BorderThickness="0,0,0,1" BorderBrush="#DDD" x:Name="parentRow">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="0,0,10,0" Width="123" MouseUp="StackPanel_MouseUp_1"> <!--Height="{Binding Height, ElementName=parentRow}"-->
<TextBlock Foreground="#4585B9" FontWeight="Bold" FontFamily="Trebuchet MS" FontSize="20" HorizontalAlignment="Right">
<Run Text="{Binding Path=Time}"/></TextBlock>
<TextBlock FontFamily="Trebuchet MS" Foreground="#808080" HorizontalAlignment="Right"><Run Text="View Bookings"/></TextBlock>
</StackPanel>
<ListView ItemsSource="{Binding Bookings}" ScrollViewer.CanContentScroll="False">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TourName}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListView>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</helpers:AnimatedScrollViewer>

To get value by its key, from other dictionary through XAML

I set ObservableCollection<Employe> as source for ListBox.ItemsSource, and set some DataTemplate for ListBox.ItemsSource.
XAML for template:
<DataTemplate x:Key="tmpEmploye">
<Border BorderThickness="3" BorderBrush="Gray" CornerRadius="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Surname}"
HorizontalAlignment="Stretch" Margin="2"
FontWeight="Bold" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Path=Name}"
HorizontalAlignment="Stretch" Margin="2"/>
<TextBlock Text="{Binding Path=Patronymic}"
HorizontalAlignment="Stretch" Margin="2"
TextWrapping="Wrap"/>
</StackPanel>
<TextBlock Text="{Binding Path=Post}" Foreground="Gray"
HorizontalAlignment="Stretch" Margin="2"
FontStyle="Italic" TextWrapping="Wrap"/>
</StackPanel>
</Border>
</DataTemplate>
XAML for ListBox:
<ListBox x:Name="lbEmployees" x:FieldModifier="internal"
ItemTemplate="{StaticResource tmpEmploye}" HorizontalContentAlignment="Stretch"/>
Result:
Look, the Employee class has a Post property, which is an index of record from ObservableDictionary<Int64, String> (my some class). You can see the '-1' values, but I need to get value for this key from my ObservableDictionary<Int64, String> object. Can I get necessary values and to display it through XAML code?
You can write a IMultiValueConverter, and bind both the Post property and your dictionary to it.
See http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx

How to set the text of textblock present inside a datatemplate of a listbox from c# codebehind?

My listbox XAML looks like this:
<ListBox x:Name="lstbxbProducts" SelectionChanged="lstbxbProducts_SelectionChanged_1" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="80" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Text=" -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="400">
<TextBlock Text="{Binding prddsc}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="180">
<StackPanel>
<TextBlock Text="{Binding um_cod}" Name="txtblkUmcode" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="20"></StackPanel>
<StackPanel Width="20">
<Image Source="/Images/Arrowselection.png" Tapped="Image_Tapped_1" VerticalAlignment="Center" ></Image>
</StackPanel>
</StackPanel>
<StackPanel Width="180">
<TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
</StackPanel>
<StackPanel Width="100">
<TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Right" ></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to set the text of Name="txtblkUmcode" present in my listbox from c# codebhind in this way, but no luck:
TextBlock txtBlk = new TextBlock();
txtBlk = lstbxbProducts.FindName("txtblkUmcode") as TextBlock;
txtBlk .Text = "test";
Please let me know how can I:
set the text of that textblock prsent inside a datatemplate of a my listbox ?
i want to change the text of that textblock of that single listbox item only ?
How can I do this? Please let me know.
You have to avoid this kind of programming practice because if the view changes - for example there comes a modified DataTemplate with other controls displaying that property(Label, TextBox, etc) - can break your application.
Sometimes there is no other solution.
See the answer here: http://msdn.microsoft.com/en-us/library/bb613579.aspx

Resources