我通过 xaml 文件制作了一个 ListBox。当用户在文本框中输入内容时,它会在列表框的行中添加一个文本框和一个按钮。
我希望该按钮成为“删除它自己的行按钮”。但是当我尝试这样做时,它不会让我访问 ListBox,因为“无法访问静态上下文中的非静态字段”。
public class GenericRowItem
{
private StackPanel _genericStackPanel = new StackPanel();
private TextBox _genericTextBoxImage = new TextBox();
private TextBox _genericTextBoxText = new TextBox();
private CheckBox _genericCheckBox = new CheckBox();
private Button _genericButton = new Button();
public GenericRowItem(String text)
{
_genericButton.Content = "X";
_genericButton.VerticalAlignment = VerticalAlignment.Top;
_genericButton.HorizontalAlignment = HorizontalAlignment.Right;
_genericButton.Width = 25;
_genericButton.Height = 25;
_genericButton.VerticalContentAlignment = VerticalAlignment.Center;
_genericButton.HorizontalContentAlignment = HorizontalAlignment.Center;
_genericButton.Click += GenericListBox.Items.... <--- THIS DOESN'T WORK, IT WONT ACCESS GENERICLISTBOX
}
public StackPanel GetStackPanel()
{
return _genericStackPanel;
}
}
我的xml代码:
<ListBox Name ="GenericListBox" HorizontalAlignment="Left" Height="195" Margin="25,345,0,0" VerticalAlignment="Top" Width="650" FontFamily="Courier New" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
</ListBox>