我有两个WPF控件,但是当我刷新它们的数据绑定时,只有其中一个正在刷新

时间:2018-09-10 20:04:44

标签: c# wpf

我有一个在WPF中创建的VSTS控件,我在页面上实例化了其中两个:

BindableGrid

他们两个都在显示适当的图像;但是,当玩家占据的当前星系在我的游戏中改变时,只有starSystemMap控件会更新; galaxyMap控件也不会更新(应该在当前占用的系统上显示飞船图标)。我看不出这些控件的绑定方式有什么不同。每当玩家飞船迫使他们刷新时,我都会调用此代码:

<Viewbox Name="galaxyViewbox" Width="400" Height="400" DataContext="{x:Static models:Galaxy.Current}">
    <local:BindableGrid x:Name="galaxyMap" ArraySource="{Binding StarSystemArray}" CellSize="16" BackgroundImage="/Images/Starfield.png">
        <local:BindableGrid.ItemTemplate>
            <DataTemplate>
                <local:TileView ImagePaths="{Binding ImagePaths}"/>
            </DataTemplate>
        </local:BindableGrid.ItemTemplate>
    </local:BindableGrid>
</Viewbox>

<Viewbox Name="starSystemViewbox" Stretch="Uniform" Grid.RowSpan="2" Grid.Column="1" DataContext="{x:Static models:StarSystem.Current}">
    <Grid>
        <local:BindableGrid x:Name="starSystemMap" ArraySource="{Binding SpaceObjectArray}" CellSize="64" BackgroundImage="/Images/Starfield.png">
            <local:BindableGrid.ItemTemplate>
                <DataTemplate>
                    <local:TileView ImagePaths="{Binding ImagePaths}"/>
                </DataTemplate>
            </local:BindableGrid.ItemTemplate>
        </local:BindableGrid>
        ...
    </Grid>
</Viewbox>

我知道还有其他代码与此交互。让我知道您是否还有其他需要解决的问题!谢谢!

编辑:共享更多细节,首先是BindableGrid.ArraySource:

galaxyMap.GetBindingExpression(BindableGrid.ArraySourceProperty).UpdateTarget();
starSystemMap.GetBindingExpression(BindableGrid.ArraySourceProperty).UpdateTarget();

和BindableGrid.OnPropertyChanged:

    /// <summary>
    /// The array to bind to. Should be a 1D or 2D array.
    /// </summary>
    public Array ArraySource
    {
        get { return (Array)GetValue(ArraySourceProperty); }
        set { SetValue(ArraySourceProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ArraySource.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ArraySourceProperty =
        DependencyProperty.Register(nameof(ArraySource), typeof(Array), typeof(BindableGrid), new PropertyMetadata(null));

编辑:这是我的ImagePaths属性,该属性不会反弹以显示每个恒星系统的最新图像:

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);

        if (e.Property == ArraySourceProperty || e.Property == ItemTemplateProperty || e.Property == CellSizeProperty || e.Property == BackgroundImageProperty)
        {
            if (ArraySource != null)
            {
                ColumnDefinitions.Clear();
                RowDefinitions.Clear();
                int width = ArraySource.GetLength(0);
                int height = ArraySource.Rank == 1 ? 1 : ArraySource.GetLength(1);
                for (var x = 0; x < width; x++)
                    ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(CellSize) });
                for (var y = 0; y < height; y++)
                    RowDefinitions.Add(new RowDefinition { Height = new GridLength(CellSize) });
                if (ItemTemplate != null)
                {
                    for (var x = 0; x < width; x++)
                    {
                        for (var y = 0; y < height; y++)
                        {
                            var item = ArraySource.Rank == 1 ? ArraySource.GetValue(x) : ArraySource.GetValue(x, y);
                            var box = Children.Cast<UIElement>().Where(b => (int)b.GetValue(RowProperty) == y && (int)b.GetValue(ColumnProperty) == x && (int)b.GetValue(RowSpanProperty) == 1 && (int)b.GetValue(ColumnSpanProperty) == 1).SingleOrDefault();
                            if (box == null)
                            {
                                box = (UIElement)ItemTemplate.LoadContent();
                                box.SetValue(ColumnProperty, x);
                                box.SetValue(RowProperty, y);
                                Children.Add(box);
                            }
                            if (item == Wormholes.Models.StarSystem.Current)
                            {

                            }
                            box.SetValue(DataContextProperty, item);
                        }
                    }
                }
            }
            if (bg != null)
                bg.Source = BackgroundImage;
            else
                bg = new Image { Source = BackgroundImage };
            if (ColumnDefinitions.Count > 0 && RowDefinitions.Count > 0)
            {
                bg.SetValue(ColumnSpanProperty, ColumnDefinitions.Count);
                bg.SetValue(RowSpanProperty, RowDefinitions.Count);
                if (!Children.Contains(bg))
                    Children.Add(bg);
            }
        }
    }
}

在移出的恒星系统上调用RefreshImagePaths,而在更改恒星系统时又移入其中。

编辑:真正有趣的是,在绑定BindableGrid时我叫public ObservableCollection<string> ImagePaths { get; set; } = new ObservableCollection<string>(); public void RefreshImagePaths() { ImagePaths.Clear(); ImagePaths.Add("Star"); if (Current == this) { foreach (var ip in PlayerShip.Instance.ImagePaths) ImagePaths.Add(ip); } } ,但未更新银河地图的UI ...

编辑:这是我的TileView.xaml.cs:

box.SetValue(DataContextProperty, item);

1 个答案:

答案 0 :(得分:0)

我的 TileView OnPropertyChanged 方法从未被调用,因为 TileView 的DataContext和 ImagePaths 从未实际上改变了;仅 ImagePaths 中的项目已更改。因此,我不得不在 BindableGrid.OnPropertyChanged 中强制刷新:

  box.DataContext = null;
box.DataContext = item;
 

我敢肯定有更好的方法可以做到这一点,但目前为止,它可以工作!