ModernWpf
ModernWpf copied to clipboard
Adding Style Property will cause either fallback to default windows style or binding issues
I have a ListView with a GridView containing a templated columns. I want to add the following style property to my listview:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" >
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.ItemContainerStyle>
This works, however the base style is now overriden and the appearance is as follows:
To keep the modern style, I added the BasedOn Tag:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{ui:StaticResource DefaultListViewItemStyle}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.ItemContainerStyle>
Now, the modern style is back, but data-binding to the DisplayMembers won't work anymore:
Is this a mistake on my end, or an issue of the framework?
Look in output panel to see if there is error or not
@ghost1372 I have no errors (databinding or other) in the output on both versions of the code above.
Can you upload a sample project?
I have thrown together a little dummy project, you can clone here: https://github.com/TheRealRolandDeschain/ModernWPFListviewIssue
(I made a lightgreen background due to the white text color, but that shouldn't distract from the issue above)
Works without problems, I noticed that you do not set IsSelected value in ControllableLedModel and ControllableLedList.Add
public ControllableLedModel(bool isselected, double _positionX, double _positionY, uint _luminosity = 0)
{
PositionX = _positionX;
PositionY = _positionY;
Luminosity = _luminosity;
IsSelected = isselected;
}
and
ControllableLedList.Add(new ControllableLedModel(true,i * 1.5, i * 2.5, (uint)(i * 10)));
@ghost1372 The binding to IsSelected
wasn't the problem (in my actual project it is set). The problem is, that the different columns are not displayed correctly. This can be seen in your screenshot as well.
If the added style property is commented out, we can see the property values of the bound ControllableLedModel
instances in the respective columns of the ListView
As soon as I add the style property, you can only see the string "ModernWPFListviewIssue.ControllableLedModel".
oh you right, remove itemcontainerStyle and use this code
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.Resources>
Oh, I'm really sorry to drag this issue out so long, but this doesn't work as well: Changes to IsSelected
don't have any effect on the ListView Selection and the same vice versa with this code.
To add a bit of context: In my actual project I have two of those Listviews, where if one of them has Items selected, I want to have the selection mirrored on the second one automatically. This is why I need the selection bound to the items....
Here is a screenshot of the actual project:
The left is a ListView on a canvas to represent a schematic overview of the actual hardware I want to control with this GUI. The right is the ListView from the dummy project. If the user selects a item on the left, I want to automatically have them selected in the right list as well (and vice versa).
Any update on this issue?
I am having a similar issue. I am trying to center the ListView items, but I am getting the same problem as above. Here is my code:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource DefaultListViewItemStyle}">
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</ListView.ItemContainerStyle>
I have this problem too. Any updates?