Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTriggerBehavior doesn't work with DataGridRow style #153

Open
ds1709 opened this issue Mar 18, 2024 · 1 comment
Open

DataTriggerBehavior doesn't work with DataGridRow style #153

ds1709 opened this issue Mar 18, 2024 · 1 comment

Comments

@ds1709
Copy link

ds1709 commented Mar 18, 2024

I have a minimal test case, where I can't make DataTriggerBehavior make to work with DataGridRow style.
I have simple view models:

    public sealed class MyViewModelList
    {
        public List<MyViewModel> Items { get; } = new List<MyViewModel>();
    }

    public sealed class MyViewModel : INotifyPropertyChanged
    {
        private Guid id;

        public Guid Id => id;

        public bool IsEven => id.ToByteArray()[15] % 2 is 0;

        public event PropertyChangedEventHandler? PropertyChanged;

        public void UpdateId()
        {
            id = Guid.NewGuid();
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Id)));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsEven)));
        }
    }

and a simple view for it:

    <DataGrid ItemsSource="{Binding Items}">
      <DataGrid.Styles>
        <Style Selector="DataGridRow">
          <Setter Property="Foreground" Value="Blue" />
          <Setter Property="i:Interaction.Behaviors">
            <Setter.Value>
              <i:BehaviorCollectionTemplate>
                <i:BehaviorCollection>
                  <ia:DataTriggerBehavior Binding="{Binding IsEven}" ComparisonCondition="Equal" Value="True">
                    <ia:ChangePropertyAction PropertyName="Foreground" Value="Red" />
                  </ia:DataTriggerBehavior>
                  <ia:DataTriggerBehavior Binding="{Binding IsEven}" ComparisonCondition="Equal" Value="False">
                    <ia:ChangePropertyAction PropertyName="Foreground" Value="Green" />
                  </ia:DataTriggerBehavior>
                </i:BehaviorCollection>
              </i:BehaviorCollectionTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </DataGrid.Styles>
      <DataGrid.Columns>
        <DataGridTextColumn Header="VM" Binding="{Binding}" />
        <DataGridTextColumn Header="Name" Binding="{Binding Id}" />
      </DataGrid.Columns>
    </DataGrid>

As you can see, by default, row foreground is Blue. When Id is changed, row foreground must be changed to Red or Green.
First of all, it's not compilled with error Unable to resolve property or method of name 'IsEven' on type 'AvaloniaApplication1.MyViewModelList'. Second, if I use cast in binding like this Binding="{Binding (vm:MyViewModel).IsEven}", it compiles successfuly, but color doesnt't canges.
Same for DataGridCell style.
Is it bug or I do somthing wrong?

@maxkatz6
Copy link
Member

Have you tried simpler x:DataType="vm:MyViewModel" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants