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

UniformGrid does not adjust layout when Columns property changes. #3082

Closed
DanWatkins opened this issue Oct 8, 2019 · 2 comments · Fixed by #3087
Closed

UniformGrid does not adjust layout when Columns property changes. #3082

DanWatkins opened this issue Oct 8, 2019 · 2 comments · Fixed by #3087

Comments

@DanWatkins
Copy link

Changes to the Columns property do not appear to trigger a layout recalculation for the UniformGrid.

In the following example, moving the slider changes the SliderValue VM property, but the changes are not reflected in the UniformGrid (the rendered columns are always at the default of 7):

<DockPanel>
    <Slider Name="slider1" DockPanel.Dock="Top" IsSnapToTickEnabled="True" Minimum="1" Maximum="10" Value="{Binding SliderValue}" TickFrequency="1"/>
    <ItemsControl Grid.Row="1" Items="{Binding TestItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Background="LightGray" Margin="5,5,5,0">
                    <TextBlock Text="{Binding .}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="{Binding SliderValue}"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</DockPanel>
public class MainWindowViewModel : ViewModelBase
{
    public List<string> TestItems { get; }

    private int _SliderValue = 7;
    public int SliderValue
    {
        get => _SliderValue;
        set => this.RaiseAndSetIfChanged(ref _SliderValue, value);
    }

    public MainWindowViewModel()
    {
        this.TestItems = new List<string>
        {
            "Test Item 1",
            "Test Item 2",
            "Test Item 3",
            "Test Item 4",
            "Test Item 5"
        };
    }
}
@jp2masa
Copy link
Contributor

jp2masa commented Oct 8, 2019

I guess it's lacking AffectsRenderAffectsMeasures. I'll fix this.

@cbagpipe
Copy link

cbagpipe commented Dec 27, 2024

Experiencing this problem too, but with different layout:

public ObservableCollection<ShellPage>? Items { get; set; }
<ItemsControl.ItemsPanel>
   <ItemsPanelTemplate>
      <UniformGrid Rows="1" Columns="{Binding Items, Converter={StaticResource CollectionToItemsCount}}"/>
   </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

Workaround:

// Fody.PropertyChanged delegate
 public void OnPropertyChanged(string propertyName, object before, object after)
 {
  if (propertyName == nameof(Items))
     {
         if (before != null && before is ObservableCollection<ShellPage> bItems)
             bItems.CollectionChanged -= ItemsChanged;

         if (after != null && after is ObservableCollection<ShellPage> aItems)
             aItems.CollectionChanged += ItemsChanged;
     }

     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 }

 private void ItemsChanged(object? sender, NotifyCollectionChangedEventArgs e)
 {
     switch (e.Action)
     {
         case NotifyCollectionChangedAction.Add:
         case NotifyCollectionChangedAction.Remove:
         case NotifyCollectionChangedAction.Reset:
             OnPropertyChanged(nameof(Items));
             break;
         case NotifyCollectionChangedAction.Move:
         case NotifyCollectionChangedAction.Replace:
         default:
             break;
     }
 }

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

Successfully merging a pull request may close this issue.

3 participants