У меня есть BindableGrid
элемент управления, который я создал в WPF, и я создал два из них на своей странице:
<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>
Оба они отображают соответствующие изображения;однако, когда текущая звездная система, занятая игроком корабля в моей игре, изменяется, обновляется только элемент управления starSystemMap;элемент управления galaxyMap также не обновляется (на текущей занятой системе должна отображаться иконка корабля).Я не вижу, что же такого особенного в том, как связаны эти элементы управления;Я вызываю этот код каждый раз, когда корабль игрока движется, чтобы заставить его обновиться:
galaxyMap.GetBindingExpression(BindableGrid.ArraySourceProperty).UpdateTarget();
starSystemMap.GetBindingExpression(BindableGrid.ArraySourceProperty).UpdateTarget();
Я знаю, что с этим взаимодействует другой код;дайте мне знать, если вам нужно еще что-нибудь увидеть, чтобы понять это!Спасибо!
изменить: поделиться более подробной информацией, сначала BindableGrid.ArraySource:
/// <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));
и BindableGrid.OnPropertyChanged:
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);
}
}
}
}
edit: и вот мое свойство ImagePaths, котороене восстанавливается для отображения последних изображений для каждой звездной системы:
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);
}
}
RefreshImagePaths вызывается для звездной системы, из которой вы выходите, и той, в которую вы переходите при смене звездных систем.
edit: действительно интересно то, что я называю box.SetValue(DataContextProperty, item);
при привязке BindableGrid, но пользовательский интерфейс не обновляется для карты галактики ...
edit: вот мой TileView.xaml.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Wormholes.Views
{
/// <summary>
/// Interaction logic for TileView.xaml
/// </summary>
public partial class TileView : UserControl
{
public TileView()
{
InitializeComponent();
}
/// <summary>
/// Paths to images to display, from back to front Z-index.
/// </summary>
public ObservableCollection<string> ImagePaths
{
get { return (ObservableCollection<string>)GetValue(ImagePathsProperty); }
set { SetValue(ImagePathsProperty, value); }
}
// Using a DependencyProperty as the backing store for ImagePaths. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImagePathsProperty =
DependencyProperty.Register(nameof(ImagePaths), typeof(ObservableCollection<string>), typeof(TileView), new PropertyMetadata(null));
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == ImagePathsProperty)
{
grid.Children.Clear();
if (ImagePaths != null)
{
foreach (var ip in ImagePaths)
{
try
{
var img = new Image { Source = new BitmapImage(new Uri(System.IO.Path.GetFullPath(System.IO.Path.Combine("Images", ip + ".png")), UriKind.RelativeOrAbsolute)), Width = Width, Height = Height };
grid.Children.Add(img);
}
catch (FileNotFoundException ex)
{
Console.Error.WriteLine($"Could not find image: {ip}");
}
catch (DirectoryNotFoundException ex)
{
Console.Error.WriteLine($"Could not find image: {ip}");
}
}
}
}
}
}
}