Binding failed for a GridViewColumn Visibility property - c#

<ListView Grid.Row="1" Margin="10" Name="lvRegistersConfig" ItemsSource="{Binding registers}">
<ListView.Resources>
<local:BoolToVisibility x:Key="BTVConverter"/>
</ListView.Resources>
<ListView.View>
<GridView>
<local:GridViewColumnExt Header="Register Name" Width="100" Visibility="{Binding Vis, Converter={StaticResource BTVConverter}}" >
<local:GridViewColumnExt.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Visibility="{Binding Vis, Converter={StaticResource BTVConverter}}"/>
</DataTemplate>
</local:GridViewColumnExt.CellTemplate>
</local:GridViewColumnExt>
</GridView>
</ListView.View>
</ListView>
GridViewColumnExt is a class that inherits from GridViewColumn and adds the Visibility property.
My ListView's ItemsSource as you may see, is set to be the registers ObservableCollection. Register class has a property of type bool named Vis.
It all works fine for the TextBox, but from the GridViewColumnExt don't, i think i cannot reach the collection and bind to the specific object.
I am not sure what's the DataContext for the GridViewColumn, i saw that i cannot set it.
I need a hint on this, how can my GridViewColumn see the Vis property from the Register object in the registers ObservableCollection ?

I'm not sure how the GridViewColumnExt control works, but it looks like with your XAML that the DataContext is not going to be individual item in the registers collection, rather it is the same DataContext on which the registers collection is declared. So, you're probably not going to be able to control the visibility of the column with the Register.Vis property.
If you're looking to put more in your cell template, you could wrap the TextBlock in a Grid and bind the Grid.Visibility property to the Register.Vis property.
Does that make sense?

Related

How can I reference the ListView DataContext from within the ListView.ItemTemplate in XAML?

I have a ListView whose DataContext is set to my ViewModel. The ListView.ItemsSource is set to a Collection Property of the ViewModel. There is a Property called MyIndex say on this ViewModel. The value of MyIndex changes during the execution of my project.
I need a way to access in XAML 'MyIndex' from within the ItemTemplate of the ListView so that I can change aspects of each ListViewItem based on the value of MyIndex.
I can't use TemplatedParent and then the .Parent property of the ListViewItem in the binding as .Parent isn't the ListView.
Here is some pseudo XAML to illustrate better what I mean.
<ListView ItemsSource="ItemsCollection">
<ListView.ItemTemplate>
<ItemContainerTemplate>
<Grid Background="{Binding <some xaml to reference MyIndex Property of ListView.DataContext to use in Converter>}">
</Grid>
</ItemContainerTemplate>
</ListView.ItemTemplate>
</ListView>
I'm not experienced with WPF.
This should do the trick. That way, you can use the DataContext of the parent in the children.
<ListView ItemsSource="ItemsCollection">
<ListView.ItemTemplate>
<ItemContainerTemplate>
<Grid Background="{Binding DataContext.MyIndex, RelativeSource={RelativeSource AncestorType=ListView}}">
</Grid>
</ItemContainerTemplate>
</ListView.ItemTemplate>
</ListView>

How to bind to a source inside a ListBox different from the ItemsSource already specified

I have a ListBox inside a HubSection, whose Items are bound to a class "players" added to my DefaulViewModel via code behind.
First I simply put a TextBox bound to the property "PlayerName" of my class "players".
Now I would like to add a ComboBox with some items that are NOT part of the class players.
Is it possible ? I thought that definind an ItemsSource in the ComboBox would sort of override the ItemsSource of the ListBox, but nothing displays.
The DataContext of the whole page is defined like so:
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
Then the HubSection is like so:
<HubSection x:Name="HubSec1">
<DataTemplate>
<ListBox x:Name="ListBox1" ItemsSource="{Binding players}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=PlayerName, Mode=TwoWay}"/>
<ComboBox ItemsSource="{Binding Path=ListOfElements}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</HubSection>
If I define the ComboBox in the same way but outside the ListBox, it will display the string elements of "ListOfElements" properly.
But in this ListBox, the ComboBox is empty. So my guess is that having defined an ItemsSource for the ListBox, it is not possible to override it.
I have tried to define a DataTemplate but was not successful doing so, but it might be the good solution (and I did not proceed properly)
What am I missing ?
Edit :
The ComboBox items is an ObservableCollection. It is not part of the "players" class.
Here is how I added these elements to the DefaultViewModel
DefaultViewModel.Add("players", players);
DefaultViewModel.Add("MyItemsList", ListOfElements);
You can walk up the visual tree and bind to an ancestors datacontext:
{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
EX:
{Binding Path=ListOfItems, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}
that should give you the datacontext that the listbox has, so assuming your ListOfItems exists in that data context.
Or you can name your control, and then bind to its datacontext by element name:
{Binding ElementName=mySourceElement,Path=ListOfItems}
It can be a little bit tricky to create a good working binding in Windows Apps. A widely used work around is to use the Tag property.
<ListBox x:Name="ListBox1" ItemsSource="{Binding players}" Margin="0,184,0,0" Tag="{Binding Path=ListOfElements}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=PlayerName, Mode=TwoWay}"/>
<TextBox Text="{Binding Path=Tag, ElementName=ListBox1}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
A binding to an element wirh the specific name will work always. And the ListOfElements should be in the scope of the ListBox so you can use the Tag property as a proxy. If you need to bind more than one property, you can also use dummy XAML elements:
<Border Tag="{Binding ...}" Name="dummy1"/>

Why does ListView Item Template show buttons but with no text?

I have an WPF application with this XAML...
<ListView
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{Binding HTMLControlNames}">
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"></Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The bindings are correct and there is data in the ObservableCollection property that implements INPC.
I added the Expression Dark Theme which is also working but this is the output from the markup above:
The buttons are there, they just aren't showing the text... BTW the number of buttons is equal to the count of items in the collection.
Here's the property in the ViewModel, single stepping shows me there are items (the proper ones) in the collection.
public ObservableCollection<ControlName> HTMLControlNames
{
get { return _HTMLControlNames; }
set
{
_HTMLControlNames = value;
PropChanged("HTMLControlNames");
}
}
Lastly the ControlName Class:
public class ControlName
{
public string Name { get; set; }
}
If I don't use ExpressionDark.xaml the content shows up!
Here's more information: Top part of the control shows up ok...
If I don't use DataTemplate as in the top half of this control the buttons are fine.
Here's the default Control Template (just a grid with a content presenter)..
What you are seeing that you think is a button in the ListView control is actually the header area. You can see this more clearly by adding columns to the header:
<ListView.View>
<GridView>
<GridViewColumn Header="Test" />
</GridView>
</ListView.View>
The list view items are actually the thin lighter-grey strips below this header.
I've not had much experience in templating list views (I usually use list boxes), but in the theme you are using, the list view seems to be templated in such a way that it basically ignores the item template. You can see that it does by using a daft template like this:
<ListView.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Orange" Width="20" Height="20" />
</DataTemplate>
</ListView.ItemTemplate>
You will see that no orange rectangles appear in the list view, even when the collection that you bind to has elements in it.
I'm not quite sure how you can get around this problem. Maybe someone with more experience of templating list view controls can chime in.
Thanks to Stephen this is the solution so far:
<ListView.View>
<GridView>
<GridViewColumn x:Name="xHtmlControlcol"
Header="Filter For:"
Width="{Binding ActualWidth,
ElementName=XListView,
Mode=OneWay}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding Name}"
Width="{Binding ActualWidth,
ElementName =XListView,
Mode=OneWay}" BorderThickness="1,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
It produces this: Note that there's still one small issue with the left margin which I don't know how to fix right now.
The Trick to this is that when using Data Binding:
Use the ItemsSource to bind to collection of ListView
Use the ListView.View as shown above to set up a GridViewColumn.
Use the GridViewColumn.CellTemplate to inject the control type you want.
Make sure the injected control is bound to the proper Content path name.

Binding to a second property

I have two properties in my viewmodel, called Premises and Towns.
I'm binding my ListViewItems to Premises, and in the itemtemplate I want to bind to Towns, but when I use the following XAML it tries to bind to Premises.Towns instead of Towns.
How can I bind to Towns directly?
Viewmodel:
public class MainWindowViewModel
{
public ObservableCollection<Premise> Premises;
public List<Town> Towns;
}
XAML:
<ListView x:Name="PremisesList" Margin="195,35,10,10"
ItemContainerStyle="{StaticResource OverviewListViewItemStyle}"
ItemsSource="{Binding Premises}" HorizontalContentAlignment="Stretch">
And this is what's in my OverviewListViewItemStyle.
<ComboBox ItemsSource="{Binding Towns}" Grid.Row="2" Grid.ColumnSpan="3">
<ComboBox.ItemTemplate>
<DataTemplate>
<ComboBoxItem>
<TextBox Text="{Binding Name}" />
</ComboBoxItem>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I'd like to be able to select a Town for a Premise via XAML.
You are correct in your assumption. ComboBox looks for Towns in Premise class, which is the class behind each ListViewItem If you want to refer to same context as ListView you need to use RelativeSource binding.
<ComboBox
ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.Towns}"
Grid.Row="2"
Grid.ColumnSpan="3"
DisplayMemberPath="Name"/>
Not related to your problem but you also don't need to specify DataTemplate to display single property. DisplayMemberPath will work as well. If you do specify DataTemplate you don't need to use ComboBoxItem as ComboBox will wrap DataTemplate content in ComboBoxItem so effectively you'll end up with ComboBoxItem inside another ComboBoxItem
You bind the ItemsSource to the Premises property therefore if you bind to the Towns in the OverviewListViewItemStyle the binding engine will look up in the Premise object for a property called Towns.
If you want to select a town for a premises you should tell to the combobox where to look from that property. You can try to set the combobox's datacontext to the main viewmodel with relative source in the binding. Something like that:
ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.Towns}"

WPF: Accessing two DataContexts in the same control

I am using an MVVM approach, and I have an object from my ViewModel called DatabasesSubFrame which is DataTemplated to show a ListBox. I want to display a Button below the ListBox, which binds to both the currently SelectedItem, and a property on the DatabasesSubFrame object which is being DataTemplated.
I know how to refer to the currently selected item, by setting the DataContext on a shared ancestor with the ListBox and use {Binding /}. In this example the shared ancestor is a StackPanel. And if the DataContext wasn't explicitly set there I could easily bind to a property on the DatabasesSubFrame object by just doing {Binding SomeProperty}. However, if I do {Binding SomeProperty} within the explicitly set DataContext, it refers to the wrong DataContext.
How do I access the "original" DataContext here? I tried messing with RelativeSources and TemplatedParents but couldn't figure out how to fit them in.
<DataTemplate DataType="{x:Type VM:DatabasesSubFrame}">
<StackPanel DataContext="{Binding Databases}" >
<ListBox Name="DbInfoBox"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding ShortName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- Problem: The Command and V:CreateCommandBinding.Command are set incorrectly here. How do I access OpenDbCommand from the top-level DataTemplate's DataContext? -->
<Button Content="Open Database"
CommandParameter="{Binding /}"
Command="{Binding ???, Path=OpenDbCommand.Command}"
V:CreateCommandBinding.Command="{Binding ???, Path=DataContext.OpenDbCommand}"/>
</StackPanel>
</DataTemplate>
I think this question will help you to find the answer to yours. Another trick is to set the Name of the Window to something like "Root". You can then get at the window's original datacontext by using:
{Binding ElementName=Root, Path=DataContext.MyViewModelsProperty}

Categories