Open ds1709 opened 5 months ago
Have you tried simpler x:DataType="vm:MyViewModel"
?
Hi, @maxkatz6 . Sorry to bother you but I have the same issue . And I found it affects all ItemsControl.
For example, add a behavior to Avalonia.Xaml.Behaviors\samples\BehaviorsTestApplication\Views\Pages\EditableListBoxView.axaml
(See full code bellow). It will show an error Avalonia error AVLN:0004: Unable to resolve property or method of name 'Name' on type 'BehaviorsTestApplication.ViewModels.MainWindowViewModel'.
.
It indicates the datacontext of the Binding
in the DataTriggerBehavior
is the datacontext of MainWindow
(MainWindowViewModel
), not the datacontext of the ListBoxItem
(ItemViewModel
). There should be something wrong. You mentioned x:DataType
but I don't know how to use it in this situation. Could you add more details?
<UserControl x:Class="BehaviorsTestApplication.Views.Pages.EditableListBoxView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:BehaviorsTestApplication.ViewModels"
xmlns:views="clr-namespace:BehaviorsTestApplication.Views"
x:CompileBindings="True" x:DataType="vm:MainWindowViewModel"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450">
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
<ListBox ItemsSource="{Binding Items}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="Interaction.Behaviors">
<Setter.Value>
<BehaviorCollectionTemplate>
<BehaviorCollection>
<DataTriggerBehavior Binding="{Binding Name}" Value="Third Item">
<ChangePropertyAction PropertyName="Foreground" Value="Red" />
</DataTriggerBehavior>
</BehaviorCollection>
</BehaviorCollectionTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Styles>
<ListBox.ItemTemplate>
<DataTemplate DataType="vm:ItemViewModel" x:CompileBindings="False">
<views:ItemView />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
I have a minimal test case, where I can't make
DataTriggerBehavior
make to work withDataGridRow
style. I have simple view models:and a simple view for it:
As you can see, by default, row foreground is
Blue
. WhenId
is changed, row foreground must be changed toRed
orGreen
. First of all, it's not compilled with errorUnable to resolve property or method of name 'IsEven' on type 'AvaloniaApplication1.MyViewModelList'
. Second, if I use cast in binding like thisBinding="{Binding (vm:MyViewModel).IsEven}"
, it compiles successfuly, but color doesnt't canges. Same forDataGridCell
style. Is it bug or I do somthing wrong?