How to bind button inside listview - wpf

I cant bind button inside listview to the GoToTask property, every other binding, include second button in the grid is work well.
<Grid>
<ListView ItemsSource="{Binding Projects}" SelectedItem="{Binding SelectedProject, Mode=TwoWay}" Grid.Row="1">
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Go">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Go" Command="{Binding Path=GoToTasks, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Height="20" Width="50" HorizontalAlignment="Right" Command="{Binding GoToTasks}" Content="Go"/>
</Grid>
Everything is inside UserControl.
What is wrong?

GridViewColumn is not part of VisualTree and therefore you are not allowed to use FindAncestor.
Similar here. Try ElementName, as far as I remember it uses LogicalTree.

Related

HOWTO Remove the extra empty space(column) on the right handside of a WPF ListView

I have an WPF ListView with three columns. I see that in my ListView appears an extra empty space(column) on the right handside and I do not know why. How can I remove this extra space using XAML?
<ListView x:Name="MyListView"
Grid.Column="0"
AlternationCount="{Binding pathList.Count}"
ItemsSource="{Binding pathList}"
IsSynchronizedWithCurrentItem="True">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn x:Name="LvItemId"
Header="#"
Width="20"
DisplayMemberBinding="{Binding (ItemsControl.AlternationIndex),
RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
<GridViewColumn Header="Path"
Width="350"
CellTemplate="{StaticResource NameColumnTemplate}"/>
<GridViewColumn Header="Edit | Delete"
Width="80"
CellTemplate="{StaticResource AddDelItemTemplate}"/>
</GridView>
</ListView.View>
</ListView>
Set the HorizontalAlignment property of the ListView to Leftto prevent it from stretching horizontally:
<ListView x:Name="MyListView"
Grid.Column="0"
AlternationCount="{Binding pathList.Count}"
ItemsSource="{Binding pathList}"
IsSynchronizedWithCurrentItem="True"
HorizontalAlignment="Left">
...

DataBinding: Disable ComboBox Item

I'm trying to apply this solution to my case. Only difference is that my ComboBox is taking its items from an enum list.
I always got a binding expression error to the property "IsProgrammabile" in the ComboBox style.
My code:
<ListView ItemsSource="{Binding SchedaSelezionata.ListaIngressi}" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="NR." DisplayMemberBinding="{Binding Numero}" />
<GridViewColumn Header="FUNCTION" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={helpers:EnumBindingSource {x:Type models:INGRESSI}}}" SelectedItem="{Binding Funzione}"
ToolTip="{Binding Descrizione}" IsEnabled="{Binding ConfigurabileDaUtente}" Width="150" >
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="IsEnabled" Value="{Binding Path=IsProgrammabile, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Note that the "IsProgrammabile" property belongs to same object as the other properties (Numero, Funzione, Descrizione, ConfigurabileDaUtente).
Setting the AncestorType to GridView or ListView doesn't help.
Can you provide the solution and explain me what I do not understand in this context?
Thanks in advance
The ComboBox itself has no property named IsProgrammabile but its DataContext may have so you should add "DataContext." to the binding path:
<Setter Property="IsEnabled" Value="{Binding Path=DataContext.IsProgrammabile,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}"/>

Disable ListView selection and hover

Usually I can disable ListView selection doing like this thread suggests - or something similar where you set the ItemContainerStyle.
However I have this ListView that is defined like this:
<ScrollViewer>
<ListView ItemsSource="{Binding List, Mode=OneWay}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumnHeader Style="{StaticResource header}"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Some data -->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridViewColumn>
...
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
And if I try to specify the ItemContainerStyle - the data in the list just disappears.
Giving the above ListView, how would I proceed just to removed the selection?
Can you try this ItemContainerStyle? This basically will not pick up any input events.
<ListView ItemsSource="{Binding List, Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>

Treeview shows random extra space

I have a treeview in wpf that is showing some strange behavior (at least I think it's strange). I don't see anywhere I can attach an image but basically on some of the items it looks like it's double spaced or has an extra 3px or so margin and on some it looks like it's single spaced. Here is the XAML defining it:
<TreeView Margin="5,0,0,5" ItemsSource="{Binding SortedCategories}" HorizontalContentAlignment="Stretch">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type COBie:CategoryData}" ItemsSource="{Binding SortedTypes}">
<CheckBox IsChecked="{Binding AnyChecked, Mode=TwoWay}" Content="{Binding CategoryName}" IsEnabled="{Binding HasTypes}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type COBie:InstanceData}">
<CheckBox IsChecked="{Binding isChecked, Mode=TwoWay}" Content="{Binding ElemID}" />
</DataTemplate>
<DataTemplate DataType="{x:Type COBie:TypeData}">
<TreeViewItem IsEnabled="{Binding HasInstances}" HorizontalContentAlignment="Stretch">
<TreeViewItem.Header>
<CheckBox IsChecked="{Binding isChecked, Mode=TwoWay}" Content="{Binding ElemName}" />
</TreeViewItem.Header>
<ListView ItemsSource="{Binding SortedInstances}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding isChecked, Mode=TwoWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElemID}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding ElemName}" />
<GridViewColumn Header="Mark" DisplayMemberBinding="{Binding Mark}" />
<GridViewColumn Header="Room" DisplayMemberBinding="{Binding RoomDisplay}" />
</GridView>
</ListView.View>
</ListView>
</TreeViewItem>
</DataTemplate>
</TreeView.Resources>
</TreeView>
The only other relevant style that would apply is:
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
</Style>
I don't see anywhere that defines a margin at all and even if it did all of the elements are of the same class so it should apply to all equally.
Anyone know what is going on?
I had the same issue, that there were seemingly random gaps.
After looking closer I found out that the gaps appeared around items, that have underscore(s) in the name.
If you put the string directly in the content property of the CheckBox and it contains underscores, the CheckBox will surround a TextBlock with a Control named AccessText, probably to support short cut keys (similar to underscores in Button's Content property).
Now for some reason the Margin of the Checkbox is set on the TextBlock perfectly fine, if there is no AccessText involved, but is not set on the TextBlock inside the AccessText. This way the TextBlock inside the AccessText will have default TextBlock Style, which in my case was with Margin=5.
I guess in your case it must be a closely related issue.

Binding a command in a datagrid

I am trying to bind a button withing datagrid to a custom command.
But for some reason, the command dont get executed.
This is the XAML:
<Grid Name="LayoutRoot">
<ListView Name="ListView1"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=id,Mode=OneWay}"
Margin="-6,0,-6,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=name,Mode=OneWay}"
Margin="-6,0,-6,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Path" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=path,Mode=OneWay}"
Margin="-6,0,-6,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Update" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding id}" Command="{Binding ???????????}" Content="Edit"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
What should I write instead of the ?????????? in order to show a message with the ID.
Found a solution here:
WPF DataGrid - Button in a column, getting the row from which it came on the Click event handler
I think problem is in Cells DataContext. Within DataTemplate it does not see parent context. Try to add your command to Resources and do something like:
<DataTemplate>
<Button CommandParameter="{Binding id}" Command="{StaticResource yourCommand}" Content="Edit"/>
</DataTemplate>
Create a command Inherit from ICommand interface.
public class ShowMessageWithIdCommand : ICommand
{
// Implement it members
}
in your viewmodel:
public ICommand ShowMessageWithIdCommand
{
get{return new ShowMessageWithIdCommand(...);}
}
Then your ????? will be: ShowMessageWithIdCommand
More detail you can find here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Resources