Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将绑定的空对象更改为某些数据 问题_C#_Wpf_Xaml_Data Binding_Binding - Fatal编程技术网

C# 将绑定的空对象更改为某些数据 问题

C# 将绑定的空对象更改为某些数据 问题,c#,wpf,xaml,data-binding,binding,C#,Wpf,Xaml,Data Binding,Binding,我有一些对象的二维数组。为了获得更大的灵活性,它是一个接口类型数组。开始时,某些单元格可以为空,但稍后会填充数据。我将此数组绑定到WPF中的UniformGrid。当我创建一个新对象并将其添加到数组中时,我的网格不会改变。问题是如何设计bindind,如果绑定对象发生变化(而不是对象中的属性),网格也会发生变化 类似的问题也出现了 但没有决定 我的代码 阵列单元 (带有方法的简单类,可以更改属性以确保绑定正常工作。这里没有什么特别之处) 主类 我用数据随机填充数组,将其绑定到UniformGri

我有一些对象的二维数组。为了获得更大的灵活性,它是一个接口类型数组。开始时,某些单元格可以为空,但稍后会填充数据。我将此数组绑定到WPF中的UniformGrid。当我创建一个新对象并将其添加到数组中时,我的网格不会改变。问题是如何设计bindind,如果绑定对象发生变化(而不是对象中的属性),网格也会发生变化

类似的问题也出现了 但没有决定

我的代码 阵列单元 (带有方法的简单类,可以更改属性以确保绑定正常工作。这里没有什么特别之处)

主类


我用数据随机填充数组,将其绑定到UniformGrid中,并为空值对象提供回退值。当我向数组中添加一个新对象时,没有任何变化。如何解决此问题?

请尝试使用
可观察集合,而不是使用下面的
列表

公共列表列表层{get;private set;}

在任何时候,如果需要向集合添加对象,只需为ViewModel(DataContext)类需要实现的
INotifyPropertyChanged
接口引发
PropertyChanged
事件

interface IOrg
{
    string Text { get; }
    SolidColorBrush SolidColor { get; }
    void SetColor();
    void SetText();
}

class IOrgA : IOrg
{
    Random random;

    public IOrgA(Random random)
    {
        SolidColor = new SolidColorBrush();

        this.random = random;
        SetColor();
        SetText();
    }

    public string Text { get; private set; }
    public SolidColorBrush SolidColor { get; private set; }

    public void SetColor()
    {
        SolidColor.Color = Color.FromRgb(255, 255, (byte)random.Next(256));
    }

    public void SetText()
    {
        Text = random.Next(1000).ToString();
    }
}

class IOrgB : IOrg
{
    Random random;

    public IOrgB(Random random)
    {
        SolidColor = new SolidColorBrush();

        this.random = random;
        SetColor();
        SetText();
    }

    public string Text { get; private set; }
    public SolidColorBrush SolidColor { get; private set; }

    public void SetColor()
    {
        SolidColor.Color = Color.FromRgb((byte)random.Next(256), 255, 255);
    }

    public void SetText()
    {
        Text = random.Next(1000, 2000).ToString();
    }
}
class Table
{
    //array with data
    private IOrg[,] _layer;
    private Random _random = new Random();

    public Table(int heigth, int width)
    {
        Height = heigth;
        Widht = width;

        _layer = new IOrg[heigth, width];
        ListLayer = new List<List<IOrg>>();

        FillLayer();
        FillList();
    }

    public int Height { get; private set; }
    public int Widht { get; private set; }
    //list for binding
    public List<List<IOrg>> ListLayer { get; private set; }

    void FillLayer()
    {
        for (int i = 0; i < Height; i++)
        {
            for (int j = 0; j < Widht; j++)
            {
                //randomly fill array
                if (_random.Next(30) == 1)
                {
                    IOrg org;

                    if (_random.Next(2) == 0)
                    {
                        org = new IOrgA(_random);
                    }
                    else
                    {
                        org = new IOrgB(_random);
                    }

                    _layer[i, j] = org;
                }
            }
        }
    }
}
<Window.Resources>

    <!-- size of uniform gris -->
    <sys:Int32 x:Key="GridSize" >50</sys:Int32>

    <!-- template for each row -->
    <DataTemplate x:Key="RowDataTemplate">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource ElementDataTemplate}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid x:Name="RowGrid" Columns="{StaticResource GridSize}"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>

    <!-- template for each element in row -->
    <DataTemplate x:Key="ElementDataTemplate">
        <TextBlock Text="{Binding Text, FallbackValue=__}">
            <TextBlock.Background>
                <SolidColorBrush Color="{Binding SolidColor.Color, FallbackValue=#123456}"></SolidColorBrush>
            </TextBlock.Background>
        </TextBlock>
    </DataTemplate>

</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="14*" />
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>

    <ItemsControl Grid.Row="0" x:Name="lst"  ItemTemplate="{DynamicResource RowDataTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid x:Name="World" Rows="{StaticResource GridSize}" Background="Bisque"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

    <Button Grid.Row="1" Width="100" Height="50" Click="Button_Click">
        button
    </Button>
</Grid>
table = new Table(50, 50);
lst.ItemsSource = table.ListLayer;