C#/ WPF - 通过* .CS文件访问在XAML中创建的列表框

时间:2013-09-25 18:51:35

标签: c# wpf xaml button listbox

我通过xaml文件创建了一个ListBox。当用户在文本框中输入内容时,它会向ListBox的Row添加一个文本框和一个按钮。

我希望该按钮成为“删除它自己的行按钮”。但是当我尝试这样做时,它不会让我访问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;
        }
    }

我的xaml代码:

<ListBox Name ="GenericListBox" HorizontalAlignment="Left" Height="195" Margin="25,345,0,0" VerticalAlignment="Top" Width="650" FontFamily="Courier New" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            </ListBox>

0 个答案:

没有答案