-1

I want to make a chess game in WPF, I decided to use listview to display my fields. I have a class named Field with properties. I have a list containing 64 fields and i changed to style of my listview in xaml so its look like a chess field. However, i have problem with the datatriggers. I need to change the picture (that will show the chess piece) when a property in that element in my list is changed.Nothing happens when the 'hasPiece' property is changed. I use INotifyPropertyChanged so i think i have no problem with the UI refresh, because i can update any displayed data in labels and testboxs. That is why i know i successfully changed the property when i selected 1 item, because i binded to a label. So when i select a field(an item) the 'hasPiece' property is updated to hidden, but the image still remains visible. I dont know much about datatriggers and I dont know what i did wrong.

 <ListView Grid.Row="1" Grid.Column="1"   ItemsSource="{Binding Fields}" Width="410"  SelectedItem="{Binding selectedField,Mode=TwoWay}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Height" Value="50"></Setter>
                <Setter Property="Width" Value="50"></Setter>
                <Setter Property="Margin" Value="0"></Setter>
                <Setter Property="Template">
                    <Setter.Value> 
                        <ControlTemplate TargetType="ListViewItem">
                            <Border Name="Border" BorderThickness="4,4,4,4" Background="{Binding Brush}" BorderBrush="{Binding Brush}">
                                <Grid Name="Grid" Background="{Binding Brush}">
                                    <Image Name="PieceImage"  Height="40" Width="40" Source="Images/something.jpg" >
                                        
                                    </Image>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="Grid" Property="Background" Value="{Binding MouseOverBrush}"/>
                                    <Setter TargetName="Border" Property="BorderBrush" Value="{Binding MouseOverBrush}"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Border" Property="BorderBrush" Value="Red"></Setter>
                                 
                                </Trigger>
                                <DataTrigger Binding="{Binding hasPiece}" Value="Hidden">
                                    <Setter TargetName="PieceImage" Property="Visibility" Value="Hidden">
                                        
                                    </Setter>
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                    
                    
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>

This is what i currently do when an item is selected:

private Field selected { get; set; }
        public Field selectedField
        {
            get { return selected; }
            set
            {
                selected = value;
              //  fieldId = selected.ID.ToString();
                removeImage(selected.ID);
            }
        }
     

     public void removeImage(int id)
    {
        foreach (Field item in Fields)
        {
         
            if (item.ID == id)
            {
                
                item.hasPiece = System.Windows.Visibility.Hidden;
                fieldId = item.hasPiece.ToString();
               
            }
        }
    }

I currently just want to test everything that needed, i know later i have to change things, but now i just want to know how i can change something in the style with datatriggers properly.

3
  • How does your 'Field' type look? Commented Jun 12, 2022 at 16:53
  • @OneBigQuestion Im on mobile now, so i cant copy paste, but it has an id(int), two brushes which are binded as you can see and a hasPiece property which is a visibility. I may change and add things later, but now i just want to know how to use a datatrigger for that picture which is inside the template setter Commented Jun 12, 2022 at 21:07
  • 1
    Are you sure that Value="Hidden" is correct for a DataTrigger on your hasPiece property? The name strongly suggests a boolean property. Commented Jun 13, 2022 at 6:06

1 Answer 1

1

I solved the problem!

I implemented the INotifyPropertyChanged interface to my ViewModel, so the UI got updated when a variable which was created there is changed or when I added or removed items to my Observable collection which was also created there. However if I want to edit my collection I have to implement this interface to my model. My problem was not related to DataTrigger. My problem was the lack of INotifypropertyChanged in my model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.