0

Following is the structure of my data:

public class TokenItems 
{ 
    public int ID { get; set; } 
    public int itemID { get; set; }
    public string name { get; set; } 
    public int qty { get; set; } 
    public int twQty { get; set; } 
    public bool readyStatus { get; set; } 
    public Nullable<DateTime> orderOn { get; set; } 
    public int styleType { get; set; } 
}

public class Token 
{ 
    public int tokenNo { get; set; } 
    public Nullable<DateTime> startedOn { get; set; } 
    public List<TokenItems> tokenItems { get; set; } 
    public bool readyStatus { get; set; } 
    public bool acceptStatus { get; set; } 
}

The above structure fits well in the following DataTemplate. (It has multiple data template, DataTemplate within the DataTemplate)

TokenPanel has the data from Token class which is assigned from code behind like this:

TokenPanel.ItemsSource = List<Token> filledList;

tokenItems is assigned within the XAML:

ItemsSource = {Binding List<TokenItems> tokenItem}

The Template of tokenItems further contains the template of buttons which are created from the list items of the tokenItem.

I have applied custom style (redButton) to the button with Click event (listClick).

<ScrollViewer>
            <ItemsControl x:Name="TokenPanel">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="0.119*"/>
                                <RowDefinition Height="0.881*"/>
                            </Grid.RowDefinitions>
                            <TextBlock TextWrapping="Wrap" Text="{Binding tokenNo}" />
                            <StackPanel Grid.Row="1" Width="Auto" HorizontalAlignment="Stretch">
                                <ItemsControl Height="Auto" ItemsSource="{Binding tokenItems}" HorizontalAlignment="Stretch" Width="Auto">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Button Height="38" Width="Auto" Style="{StaticResource redButton}" HorizontalContentAlignment="Stretch" Click="listClick" >
            <TextBlock Text= "{Binding name}"/>
                                            </Button>

                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>

                                </ItemsControl>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" x:Name="tokenListBox" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>

The program compiles well without any error. However when I update the TokenList it shows error with NullReference exception somewhere in XAML parsing.

I tried removing the Click event property from the XAML without any click event assigned. This executed program very well. But I can't click on the button which is the most thing I want. Further, I need custom style as well.

I am unable to figure out what the problem is... A quick fix will be highly appreciated.

EDIT:

This updates the tokenList;

 public void update() {
        List<TokenItems> tempList = new List<TokenItems>();
        tokenList.Clear(); // clears previous items in tokenList;
        while(database.Read()) {
                tempList.Add(new Token() { 
                           item= (int)database["item"]
                        });
        }

           var temp = new List<BumpBar.TokenItems>();
           temp.AddRange(tokenModify(tempItems)); // modifies the items within the list
           tempItems.Clear(); // clear to refill the tempItems
           tempItems.AddRange(temp);

           tokenList.Add(new Token() { 
                     tokenNo = tokensList[i].tokenNo, 
                     tokenItems = tempItems, 
                      startedOn = tokensList[i].startedOn 
                    });
  }
4

1 回答 1

0

发布与此评论相关的代码。我猜您不小心使该令牌列表无效。

该程序编译良好,没有任何错误。但是,当我更新 TokenList 时,它会在 XAML 解析中的某处显示 NullReference 异常错误。

于 2012-09-11T12:31:28.503 回答