gpt4 book ai didi

c# - ItemsControl 文本框名称在 .cs 文件中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:17 25 4
gpt4 key购买 nike

我的 WPF 应用程序代码在 .cs 文件中定义的 函数调用 上生成面板。代码中使用了 ItemControl 来生成这些 Panels 。我想命名在此 ItemControl 中定义的文本框并在代码中使用它。我将其命名为 textEdit1 并在代码中使用它,但代码生成错误 textEdit1 不存在。谁能解决我的问题?这里的代码是:

XAML 文件:

<dxlc:ScrollBox>
<ItemsControl Name="lstPanels">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="vertical">
<Grid>
<dxe:TextEdit Height="165" Text="{Binding Text,
Mode=TwoWay}" x:Name="textEdit1"/>
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</dxlc:ScrollBox>

.CS 文件

public partial class Window1 : Window
{
string valuu;
public Window1()
{
InitializeComponent();
addPanel("Header1");
addPanel("Header2");
addPanel("Header3");
lstPanels.ItemsSource = panels;

}
public ObservableCollection<MyPanel> panels = new ObservableCollection<MyPanel>();
public void addPanel(string buttonId)
{
MyPanel p = new MyPanel { Id = buttonId};
panels.Add(p);
functionb(p);
}
public void functionb(MyPanel obj)
{
valuu = obj.Text;
}

private void button2_Click(object sender, RoutedEventArgs e)
{
foreach (var f in panels.ToList())
{
MessageBox.Show( f.Id + " *** " + f.Text);
}
}
}

public class MyPanel : INotifyPropertyChanged
{
private string _id;
private string _text;

public string Id
{
get { return _id; }
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged();
}
}
}
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
NotifyPropertyChanged();
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged( String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

最佳答案

我看到您正在为您的 TextBox 和 ScrollBox 使用一些第 3 方库。如果您向我提供库的名称,我可以查看它们,因为功能可能与 WPF 开箱即用的功能不同。
至于现在你有 3 个选项(我给出了标准 TextBox 和 ItemsControl 的例子):

I) 您根本不必访问文本框。
此处描述了一种简单的解决方法:StackOverflow post

II) 在后台代码中处理事件和对 TextBoxes 的引用

  1. Loaded 事件添加到您的 TextBox:

    <TextBox x:Name="txtText" Width="300" Height="100" Loaded="txtText_Loaded" />
  2. 向您的 MyPanel 类添加一个字段以保存对 TextBox 的引用:

    public class MyPanel
    {
    public string Text { get; set; }
    public TextBox TextBox { get; set; }
    /* the rest ... */
    }
  3. 在带有面板的列表旁边的窗口中添加一个计数器:

    protected ObservableCollection<MyPanel> panels = new ObservableCollection<MyPanel>();
    private int counter = 0;
  4. 处理 TextBox 的 Load 事件:

    private void txtText_Loaded(object sender, RoutedEventArgs e)
    {
    panels[counter].TextBox = (TextBox)sender;
    counter++;
    }
  5. 如果您想访问特定的文本框,请按以下方式进行:

    MessageBox.Show(panels[i].TextBox.Text);

III) 为 FontSize 添加额外的绑定(bind):

  1. 将 FontSize 属性添加到您的 MyPanel 类:

    private double _fontSize = 10;
    public double FontSize
    {
    get { return _fontSize; }
    set
    {
    if (value != _fontSize)
    {
    _fontSize = value;
    NotifyPropertyChanged();
    }
    }
    }
  2. 将刚添加的属性绑定(bind)到 ItemsControl 中的 TextBox:

    <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}"
    FontSize="{Binding FontSize, Mode=OneWay}" />
  3. 将 slider 添加到模板并将其绑定(bind)到同一属性:

    <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />

这样,如果您更改 slider 上的值,它将更改绑定(bind)到面板的 MyPanel 对象中的值。这反过来将改变文本框的字体大小。

我测试它的整个代码看起来像这样:

<ItemsControl x:Name="lstItems" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" />
<Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

和代码背后:

public partial class MainWindow : Window
{
protected ObservableCollection<MyPanel> texts = new ObservableCollection<MyPanel>();

public MainWindow()
{
InitializeComponent();

texts.Add(new MyPanel() { Text = "Test 1" });
texts.Add(new MyPanel() { Text = "Test 2" });

lstItems.ItemsSource = texts;
}
}

public class MyPanel : INotifyPropertyChanged
{
private string _id;
private string _text;
private double _fontSize = 10;

public string Id
{
get { return _id; }
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged();
}
}
}
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
NotifyPropertyChanged();
}
}
}
public double FontSize
{
get { return _fontSize; }
set
{
if (value != _fontSize)
{
_fontSize = value;
NotifyPropertyChanged();
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

我个人会选择最后一个解决方案。
但是,请再次告诉我您正在使用哪些库,我会在有空的时候查看它们。祝你好运。

关于c# - ItemsControl 文本框名称在 .cs 文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19180753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com