[英]Cannot apply Click event to the button with the custom button style
以下是我的數據結構:
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; }
}
上面的結構非常適合以下DataTemplate。 (它具有多個數據模板,即DataTemplate中的DataTemplate)
TokenPanel具有來自Token類的數據,該數據是從后面的代碼分配的,如下所示:
TokenPanel.ItemsSource = List<Token> filledList;
tokenItems在XAML中分配:
ItemsSource = {Binding List<TokenItems> tokenItem}
tokenItems的模板還包含從tokenItem的列表項創建的按鈕模板。
我已經將自定義樣式(redButton)應用於具有Click事件(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>
該程序編譯良好,沒有任何錯誤。 但是,當我更新TokenList時,它在XAML解析中的某個地方顯示NullReference異常的錯誤。
我嘗試從XAML刪除Click事件屬性,但未分配任何click事件。 這個執行的程序很好。 但是我無法單擊最想要的按鈕。 此外,我還需要自定義樣式。
我無法弄清楚問題出在哪里...快速修復將不勝感激。
編輯:
這將更新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
});
}
發布與此評論相關的代碼。 我猜您不小心使該令牌列表無效。
該程序編譯良好,沒有任何錯誤。 但是,當我更新TokenList時,它在XAML解析中的某個地方顯示NullReference異常的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.