Databinding Class to Silverlight controls tooltip - silverlight

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 :)

Related

WPF:- How to create a Treeview control with combo box, check box and text box in it using MVVM

I'm trying to put a ComboBox, a checkbox and a few textboxes inside a treeview in WPF.
Following is the basic structure of the treeview requirement
-Parent
--Label Textbox
--Label Textbox
--Label Textbox
--Label Combo box
--Label Check box
--Payload
---Label
----Label Textbox
----Label Combobox
----Label Textbox
Following is the xaml code
<TreeView x:Name="BTreeView" Grid.Column="1" Grid.Row="1" Margin="1,10,0,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding TreeViewListings}" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type viewmodel:TreeViewBViewModel}" ItemsSource="{Binding Positions}" >
<TextBlock Text="Level1"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding PayLoadList}" DataType="{x:Type viewmodel:Position}">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 1" />
<TextBox Text="{Binding Path=Text1, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="10,0,0,0" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 2" />
<TextBox Text="{Binding Path=Text1, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="10,0,0,0" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 3"/>
<ComboBox Width="100" Height="18" Margin="10,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="10" IsEditable="True" IsReadOnly="True" BorderThickness="0">
<ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
<ComboBoxItem>Item 5</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<CheckBox Canvas.Left="10" Canvas.Top="10" Content="Active" IsChecked="False"/>
</StackPanel>
<TextBlock Text="{Binding PayLoadText}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding PayLoadData}" DataType="{x:Type viewmodel:Payload}">
<Label Content="{Binding FieldName}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type viewmodel:PayloadData}">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text 4" />
<TextBox Text="{Binding Path=Text4, UpdateSourceTrigger=PropertyChanged}" Width="200" Margin="10,0,0,0" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Data}" />
<TextBox Text="{Binding Path=DataFieldValue, UpdateSourceTrigger=PropertyChanged}" Width="200" BorderThickness="0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Operation" />
<ComboBox Width="100" Height="18" Margin="10,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="10" IsEditable="True" IsReadOnly="True" BorderThickness="0">
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
</ComboBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
I am using MVVM architectural pattern to achieve this goal. The problem that I am facing is with the arrow head of second level node. It is not aligned at the right position. The arrowhead gets automatically aligned to "Text3" combo box instead of "Payload" node. Following is the image of my tree view
I do not know if there is any other approach to include Combo box, check box, text boxes inside a treeview using MVVM approach. Would appreciate if you let me know your approach of designing this treeview

Length of ListView relative to the size of window(Visual Studio extension)

I have developed one Visual Studio extension. The problem that I am encountering is the ListView as marked in the snapshot should not go below a certain point. The result here is correct:. The ListView uses as much space as required to display 10 items. But when I shrink the window(Visual Studio extension) as shown here:, the Next button is over the ListView & Previous button is under the ListView. The expected result should be that in the second snapshot, the ListView should not go below 10 pixel above the Next & Previous buttons (as marked by green line). It already has scroll bar. How to solve this problem? Kindly ask for more information if needed.
Update : Here is the XAML as asked in comment:
<UserControl x:Class="A.B.C"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:A.B.SE.Manager"
mc:Ignorable="d"
DataContextChanged="UserControl_DataContextChanged" >
<Grid>
<ListView Margin="10,195,10,10" Name="questionListView" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=SEQuestions}" VerticalAlignment="Top">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Expander Cursor="Hand">
<Expander.Header>
<TextBlock TextWrapping="Wrap" FontSize="16" Foreground="#FF81B9F1">
<Run Text="{Binding Path=title}"/>
</TextBlock>
</Expander.Header>
<StackPanel>
<TextBlock Text="{Binding Path=body_markdown}" Foreground="#FFB2AAAA" TextWrapping="Wrap"/>
<WrapPanel>
<Expander>
<Expander.Header>
<TextBlock>
<Run Text="{Binding Path=comment_count, StringFormat='\{0} comment'}"/>
</TextBlock>
</Expander.Header>
<ListView Name="commentListView" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=comments}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=body_markdown}"/>
<Separator/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
<Expander>
<Expander.Header>
<TextBlock>
<Run Text="{Binding Path=answer_count, StringFormat='\{0} answer'}"/>
</TextBlock>
</Expander.Header>
<ListView Name="answerListView" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=answers}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=body_markdown}"/>
<Separator/>
<Expander>
<Expander.Header>
<TextBlock>
<Run Text="{Binding Path=comment_count, StringFormat='\{0} comments'}"/>
</TextBlock>
</Expander.Header>
<ListView Name="commentsOnAnswerListView" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=comments}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=body_markdown}"/>
<Separator/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
</WrapPanel>
<TextBlock Text="{Binding Path=last_activity_date, StringFormat='Last activity at {0}'}"/>
</StackPanel>
</Expander>
<Separator/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Next" Margin="10,740,10,10" HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Click="nextButtonClick"/>
<Button Content="Previous" Margin="200,740,10,10" HorizontalAlignment="Right" VerticalAlignment="Top" Width="60" Click="previousButtonClick"/>
</Grid>
</UserControl>

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

Binding data to the Listbox where ListBox has textblocks

<ListBox Height="498" Margin="2,0,0,0" Name="listBox1" Width="879" ItemsSource="{Binding}" >
<ListBoxItem >
<StackPanel Width="418" Orientation="Horizontal">
<TextBlock Name="MedicineName" Text="Alamoxy"
FontWeight="Bold" FontSize="18"
Margin="5" Width="205" >
</TextBlock>
<TextBlock Name="ListBoxLetter" Text="Amoksilin"
FontSize="18" Margin="0" Width="255" Height="23">
</TextBlock>
</StackPanel>
</ListBoxItem>
</ListBox>
I want to bind data to the listbox.
Textblock will show seprate fields. How i can do it?
please Help me about this/
Use an ItemTemplate to make it look something like this.
<ListBox Width="400" Margin="10" ItemsSource="{Binding myItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=MedicineName}" />
<TextBlock Text="{Binding Path=ListBoxLetter}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

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