DataContext of parent - wpf

I am a newbie WPF. I need to acheive the following: I have a ModelView which contains Observable collection of class "Edata". Edata also contains another ObservableColelction of Class"eParams" which contains 4 properties.
now I have listbox which contains the list of Edata, and another listview which contains the params .
Every thing works fine. the challenge is the tool tip. I have in the Edata Class Property called AsStringToolTip. I use this property to give some hint to user and briefed info about the row where the mouse is over.
<ListBox x:Name="lbx1" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding EData}" VerticalAlignment="Center" HorizontalAlignment="Center">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource Description}" TextWrapping="Wrap">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} , {1}">
<Binding Path="Edata.category" />
<Binding Path="Edata.EId" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- No Compar ListView -->
<ListView Grid.Column="1" Grid.Row="1" ItemsSource="{Binding SelectedItem.Edata.eparams ,ElementName=lbx1}" Grid.IsSharedSizeScope="True" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
<GridView >
<GridViewColumn Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Name}">
<TextBlock.ToolTip>
**<TextBlock DataContext="{Binding SelectedValue,ElementName=lbx1}" Text="{Binding Path=AsStringToolTip}">**
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ValueString" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding ValueString}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="value" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding value}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="paramtype">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding paramtype}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I hope I am clear enough. any Advise. Currently the tool tip shows nothing!!!

Since the Tooltip resides in its own visual tree it cannot find the ListBox when the binding is evaluated.
But you could bind the Tag property of the TextBlock to the ListBox and then bind the element in the Tooltip to the PlacementTarget of the ToolTip itself. It's probably better explained with some sample markup:
<GridViewColumn Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="Name" Tag="{Binding ElementName=lbx1}">
<TextBlock.ToolTip>
<ToolTip>
<TextBlock Text="{Binding PlacementTarget.Tag.SelectedItem.AsStringToolTip,
RelativeSource={RelativeSource AncestorType=ToolTip}}" />
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The PlacementTarget in the binding path refers to the "Name" TextBlock.
The Tag property of this TextBlock returns a reference to the "lbx1" ListBox.
You can then get the AsStringToolTip property of the currently selected Edata object.

Related

WPF-Show ToolTip On ListGridview with Custom Style

I am New in WPF. I have used listview in my application below is XAML:-
<ListView x:Name="lstviewMeters" Grid.Row="2" ItemContainerStyle="{StaticResource MeterRowStyle}" Height="260" Margin="0,73,31,0" VerticalAlignment="Top" Width="597" AlternationCount="2" ItemsSource="{Binding}" HorizontalAlignment="Right">
<ListView.View>
<GridView >
<GridViewColumn Header="Feeder" Width="125" DisplayMemberBinding="{Binding MeterName}" >
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Feeder" Padding="9,4,3,3"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" Width="120" DisplayMemberBinding="{Binding MeterID}">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="ID" Padding="9,4,3,3"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Now I want to Show ToolTip On Each Cell Of ListviewGridview. What is best way to Show Custom Style ToolTip On cell. I googled but not getting proper solution for the same
Is there any solutionfor the same
Create CellTemplate then add tooltip to TextBlock
<ListView x:Name="lstviewMeters" Grid.Row="2" ItemContainerStyle="{StaticResource MeterRowStyle}" Height="260" Margin="0,73,31,0" VerticalAlignment="Top" Width="597" AlternationCount="2" ItemsSource="{Binding}" HorizontalAlignment="Right">
<ListView.View>
<GridView >
<GridViewColumn Header="Feeder" Width="125">
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBlock Text="{Binding MeterName}" ToolTip="{Binding MeterName}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Feeder" Padding="9,4,3,3"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBlock Text="{Binding MeterID}" ToolTip="{Binding MeterID}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="ID" Padding="9,4,3,3"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

Adding additional padding with Width="Auto"

I have a Listview with multiple GridViewColumns, I am using Width="Auto" for each of the columns, this ensures that the columns are sized according to their largest element.
I want to add additional breathing space for each column header, something along the lines of:
Width = "Auto" + 30
I have tried setting Margins and padding however this results in the Column text heading being cut off prematurely when resized.
Any ideas on how I can achieve this ?
My GridViewColumn currently ?
<GridViewColumn Width="Auto">
<GridViewColumn.Header>
<GridViewColumnHeader HorizontalContentAlignment="Left"
Content="{x:Static resources:Resources.TableHeadingDescription}"
SizeChanged="Description_Column_SizeChanged">
</GridViewColumnHeader>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="DescriptionTextBlock"
Text="{Binding Description}"
Style="{StaticResource TextBlock_GridEntries_Style}"
Margin="{Binding HierarchyLevel, Mode=OneWay, Converter={StaticResource HierarchyToMarginConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
add ContentTemplate to GridViewColumnHeader, and in template set some Margin on element
<GridViewColumn Width="Auto">
<GridViewColumn.Header>
<GridViewColumnHeader HorizontalContentAlignment="Left"
Content="{x:Static resources:Resources.TableHeadingDescription}"
SizeChanged="Description_Column_SizeChanged">
<GridViewColumnHeader.ContentTemplate>
<DataTemplate>
<Label Margin="30,0" Content="{Binding}" />
</DataTemplate>
</GridViewColumnHeader.ContentTemplate>
</GridViewColumnHeader>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="DescriptionTextBlock"
Text="{Binding Description}"
Style="{StaticResource TextBlock_GridEntries_Style}"
Margin="{Binding HierarchyLevel, Mode=OneWay, Converter={StaticResource HierarchyToMarginConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

Item controls in wpf mvvm

I am working on wpf and using an ItemsControl to represent a list of class on the UI.
My code is like this:
<ItemsControl ItemsSource="{Binding Path=UsecaseListItems}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}"
Margin="2,5"/>
<Button Content="{Binding UsecaseName}"
Margin="2,5"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Now I wand to add a header to both the columns (Select all and UseName) at the top of the list. How can I do it??
Below is the solution (Not a good one but I supposed worse design are also possible) and please change bindings in your code:
XAML:
<ItemsControl ItemsSource="{Binding Path=list}" >
<ItemsControl.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical">
<Border BorderThickness="5" BorderBrush="DarkGray">
<StackPanel Orientation="Horizontal">
<Button Content="Select All" Width="80" HorizontalAlignment="Stretch"/>
<Button Content="User name" Width="80" HorizontalAlignment="Stretch"/>
</StackPanel>
</Border>
<Border BorderThickness="5" BorderBrush="LightGray">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=ItemsSource}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<CheckBox IsChecked="{Binding Path=DeviceState,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}"
HorizontalAlignment="Stretch" Width="100" Margin="2,5"/>
<Button Content="{Binding Name}" HorizontalAlignment="Stretch" Width="100" Margin="2,5"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</StackPanel>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
Output
Why do you want buttons as header? can't you just put buttons outside the ItemsControl?
It is preferable to go with ListView control instead of ItemsControl for what you need.
Here is the sample. .
<Grid>
<Grid.Resources>
<DataTemplate x:Key="checkboxHeaderTemplate">
<CheckBox Checked="CheckBox_Checked"> //add command for MVVM
</CheckBox>
</DataTemplate>
<DataTemplate x:Key="CheckBoxCell">
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" >
</CheckBox>
</DataTemplate>
<DataTemplate x:Key="ButtonCell">
<Button Content="{Binding Path=UsecaseName, Mode=TwoWay}" >
</Button>
</DataTemplate>
</Grid.Resources>
<ListView ItemsSource="{Binding Path=UsecaseListItems}" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}"
CellTemplate="{StaticResource CheckBoxCell}" Width="auto">
</GridViewColumn>
<GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}"
CellTemplate="{StaticResource ButtonCell}" Width="auto">
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
Hope that helps.

WPF GridView Column width on different PC resolution

How to set width for GridViewColumn that it will work all time on different resolution? For example I have this listView:
And then I open it in another PC (with different resolution) I get this view :
It is missing few column.
I copy one of my column xaml code :
<ListView x:Name="sample" Margin="30,98.4,362,150" ScrollViewer.VerticalScrollBarVisibility="Visible"
AlternationCount="2" ScrollViewer.CanContentScroll="False">
<ListView.View>
<GridView>
<GridViewColumn>
<!-- 1st Column-->
<GridViewColumnHeader Content="Product" Margin="10,0,0,0"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Grid Height="42px">
<TextBlock Text="{Binding odd}" VerticalAlignment="Center"
Width="600" TextAlignment="Left"/>
</Grid>
<TextBlock Foreground="#B30C0C" Height="42px" Padding="10,5,0,0" Text="{Binding discounText}"
Visibility="{Binding IsDiscount, Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn >
How you see I write width parameter in TextBlock. How I have to rewrite it or maybe make some changes in code?
Use relative sizing (with *) instead of fixed values. For that, you'll have to move your Width setting from the TextBlock to the Column itself:
<GridViewColumn Width="600*">
<!-- 1st Column-->
<GridViewColumnHeader Content="Product" Margin="10,0,0,0"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Grid Height="42px">
<TextBlock Text="{Binding odd}" VerticalAlignment="Center"
TextAlignment="Left"/>
</Grid>
<TextBlock Foreground="#B30C0C" Height="42px" Padding="10,5,0,0" Text="{Binding discounText}"
Visibility="{Binding IsDiscount, Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I've set 600* because the fastest way to keep the current proportions is keeping the same values you have right now, but adding the *. But that number can be a lot lower, since it just defines the proportion between columns... Feel free to toy with the values to get the effect you want.

TreeView two way binding failing at lower levels

Ok I have a custom TreeListView that inherits from TreeView and has some columns.
I have custom objects in my application with two text properties; KeyValue and Description. I'm trying to bind textboxes in the columns to these properties which works great on the initial load, however not so good when I want to change them. It works exactly as it's supposed to (changing the values on the base objects) for top level items, but for anything below that, it doesn't change the underlying values when I change the textbox value. I'm pretty new to WPF so sorry if this is basic but I have searched quite a while and can't find an answer that seems to work for me.
The XAML for the TreeListView is:
<r:TreeListView Name="TLV_Main" Margin="0,0,0,41" ItemsSource="{Binding Keynotes}" Style="{StaticResource TLV_Standard}">
<r:TreeListView.Columns>
<GridViewColumn Header="Key" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=KeyValue, Mode=TwoWay}" ContextMenu="{StaticResource Ctx_All}" ></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Description" Width="{Binding ElementName=TLV_Main, Path=ActualWidth, Converter={StaticResource DescriptionColumnConverter}}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Description, Mode=TwoWay}" TextWrapping="Wrap"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="30">
<GridViewColumn.Header>
<Image Source="pack://application:,,,/Resources/Icon_Comment.png" SnapsToDevicePixels="False" Stretch="None"></Image>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="pack://application:,,,/Resources/Icon_Comment.png" SnapsToDevicePixels="False" Stretch="None" Visibility="{Binding Converter={StaticResource BoolToVisibility}, Path=HasActiveComments}" MouseLeftButtonUp="CommentClick"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="30">
<GridViewColumn.Header>
<Image Source="pack://application:,,,/Resources/Icon_Link.png" SnapsToDevicePixels="False" Stretch="None"></Image>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="pack://application:,,,/Resources/Icon_Link.png" SnapsToDevicePixels="False" Stretch="None" Visibility="{Binding Converter={StaticResource BoolToVisibility}, Path=HasLinks}" MouseLeftButtonUp="LinkClick"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</r:TreeListView.Columns>
<r:TreeListView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildNotes}">
</HierarchicalDataTemplate>
</r:TreeListView.ItemTemplate>
</r:TreeListView>
Can anyone see why this wouldn't work right?

Resources