ItemsControl的文本框名称不工作在cs文件 [英] ItemsControl TextBox Name is not Working in .cs File

查看:78
本文介绍了ItemsControl的文本框名称不工作在cs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序code生成cs文件中定义的函数调用面板。有 ItemControl 适用于code,以生成这些面板。我想命名这个 ItemControl 定义的文本框,并使用在code。我把它命名为 textEdit1 和code,但code用它产生的误差 textEdit1 没有按'吨存在。任何人都可以解决我的问题?在这里,code为:

XAML文件:

 < dxlc:滚动框>
    < ItemsControl的名称=lstPanels>
        < ItemsControl.ItemTemplate>
            <的DataTemplate>
                < StackPanel的方向=垂直>
                    <电网>
                        < D​​XE:文本编辑高度=165文本={结合文字,
                                    模式=双向}X:名称=textEdit1/>
                    < /网格>
                < / StackPanel的>
            < / DataTemplate中>
        < /ItemsControl.ItemTemplate>
    < / ItemsControl的>
< / dxlc:滚动框>
 

.CS文件

 公共部分类窗口1:窗口
{
    串valuu;
    公共窗口1()
    {
        的InitializeComponent();
        addPanel(头1);
        addPanel(头2);
        addPanel(Header3);
        lstPanels.ItemsSource =板;

    }
    公众的ObservableCollection< MyPanel>面板=新的ObservableCollection< MyPanel>();
    公共无效addPanel(字符串buttonId)
    {
        MyPanel P =新MyPanel {n = buttonId};
        panels.Add(对);
        functionb(对);
    }
    公共无效functionb(MyPanel OBJ)
    {
        valuu = obj.Text;
    }

    私人无效button2_Click(对象发件人,RoutedEventArgs E)
    {
        的foreach(在panels.ToList变种f()的)
        {
            的MessageBox.show(f.Id +***+ f.Text);
        }
    }
}

公共类MyPanel:INotifyPropertyChanged的
{
    私人字符串_id;
    私人字符串_text;

    公共字符串ID
    {
        {返回_id; }
        组
        {
            如果(值!= _id)
            {
                _id =价值;
                NotifyPropertyChanged();
            }
        }
    }
    公共字符串文本
    {
        {返回_text; }
        组
        {
            如果(值!= _text)
            {
                _text =价值;
                NotifyPropertyChanged();
            }
        }
    }

    公共事件PropertyChangedEventHandler的PropertyChanged;

    保护无效NotifyPropertyChanged(字符串参数propertyName =)
    {
        如果(的PropertyChanged!= NULL)
        {
            的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
        }
    }
}
 

解决方案

我看到你使用一些第三方库的文本框和滚动框。如果你向我提供的库的名字,我可以看看他们的功能的情况可能与WPF有不同的开箱即装即用。
至于现在你有3个选项(我给的例子为标准文本框和的ItemsControl):

我)你没有访问文本框都没有。
它周围的一个简单的方法在这里描述:<一href="http://stackoverflow.com/questions/1492814/increasing-or-decreasing-control-fontsize-on-runtime-according-to-sliderwpf-com">StackOverflow帖子

二)处理事件,并引用文本框的背后,code

  1. 添加加载事件的文本框:

     &LT;文本框X:名称=txtTextWIDTH =300高度=100加载=txtText_Loaded/&GT;
     

  2. 字段添加到您的MyPanel类持有的引用文本框:

     公共类MyPanel
    {
        公共字符串文本{获得;组; }
        公开文本框文本框{获得;组; }
        /* 其余的部分 ... */
    }
     

  3. 添加计数器到你的窗口,旁边用面板的列表:

     受保护的ObservableCollection&LT; MyPanel&GT;面板=新的ObservableCollection&LT; MyPanel&GT;();
    私人INT计数器= 0;
     

  4. 处理文本框的Load事件:

     私人无效txtText_Loaded(对象发件人,RoutedEventArgs E)
    {
        面板[窗口] .TextBox =(文本框)发送;
        反++;
    }
     

  5. 如果您要访问一个特定的文本框,做这种方式:

     的MessageBox.show(面板[I] .TextBox.Text);
     

三)添加其他绑定字号:

  1. 一个字号属性添加到您的MyPanel类:

     私人双人_fontSize = 10;
    大众双字号
    {
        {返回_fontSize; }
        组
        {
            如果(值!= _fontSize)
            {
                _fontSize =价值;
                NotifyPropertyChanged();
            }
        }
    }
     

  2. 绑定刚刚添加的属性在你的ItemsControl文本框:

     &LT;文本框X:名称=txtTextWIDTH =300高度=100文本={结合文字;,模式=双向}
             字号={结合字号,模式=单向}/&GT;
     

  3. 添加一个滑块模板,并将其绑定到相同的属性:

     &LT;滑块最小值=10最大=30值={结合字号,模式=双向}/&GT;
     

如果您更改滑块的值通过这种方式,它会改变的值绑定到面板的MyPanel对象。这反过来会改变文本的字号。

我的整个code我测试了它的样子是:

 &LT; ItemsControl的X:名称=lstItems&GT;
        &LT; ItemsControl.ItemTemplate&GT;
            &LT;的DataTemplate&GT;
                &LT; StackPanel的方向=垂直&GT;
                    &LT;文本框X:名称=txtTextWIDTH =300高度=100文本={结合文字;,模式=双向}字号={结合字号,模式=单向}/&GT;
                    &LT;滑块最小值=10最大=30值={结合字号,模式=双向}/&GT;
                &LT; / StackPanel的&GT;
            &LT; / DataTemplate中&GT;
        &LT; /ItemsControl.ItemTemplate>
    &LT; / ItemsControl的&GT;
 

和落后code:

 公共部分类主窗口:窗口
{
    受保护的ObservableCollection&LT; MyPanel&GT;文本=新的ObservableCollection&LT; MyPanel&GT;();

    公共主窗口()
    {
        的InitializeComponent();

        texts.Add(新MyPanel(){文本=测试1});
        texts.Add(新MyPanel(){文本=测试2});

        lstItems.ItemsSource =文本;
    }
}

公共类MyPanel:INotifyPropertyChanged的
{
    私人字符串_id;
    私人字符串_text;
    私人双人_fontSize = 10;

    公共字符串ID
    {
        {返回_id; }
        组
        {
            如果(值!= _id)
            {
                _id =价值;
                NotifyPropertyChanged();
            }
        }
    }
    公共字符串文本
    {
        {返回_text; }
        组
        {
            如果(值!= _text)
            {
                _text =价值;
                NotifyPropertyChanged();
            }
        }
    }
    大众双字号
    {
        {返回_fontSize; }
        组
        {
            如果(值!= _fontSize)
            {
                _fontSize =价值;
                NotifyPropertyChanged();
            }
        }
    }

    公共事件PropertyChangedEventHandler的PropertyChanged;

    保护无效NotifyPropertyChanged(字符串参数propertyName =)
    {
        如果(的PropertyChanged!= NULL)
        {
            的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
        }
    }
}
 

我个人会去的最后的解决方案。照片 但同样,让我知道了什么​​库,你正在使用,并且我会看他们时,我有一段时间了。祝你好运。

My WPF Application code generates panels on function call defined in .cs file. There is ItemControl used in code to generates these Panels . I want to Name Textbox defined in this ItemControl and to use this in code. I named it as textEdit1 and used it in code but code generated error that textEdit1 doesn't exist. Can anyone solve my problem? Here Code is:

XAML File:

<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 FILE

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));
        }
    }
}

解决方案

I see that you are using some 3rd party libraries for your TextBox and ScrollBox. If you provide me with the names of the libraries, I could have a look at them as the functionality might be different from what WPF has out-of-the-box.
As for now you have 3 options (I am giving examples for standard TextBox and ItemsControl):

I) You do not have to access the textbox at all.
An easy way around it is described here: StackOverflow post

II) Handling events and references to TextBoxes in the code behind

  1. Add a Loaded event to your TextBox:

    <TextBox x:Name="txtText" Width="300" Height="100" Loaded="txtText_Loaded" />
    

  2. Add a field to your MyPanel class to hold a reference to a TextBox:

    public class MyPanel
    {
        public string Text { get; set; }
        public TextBox TextBox { get; set; }
        /* the rest ... */
    }
    

  3. Add a counter to your window, next to a list with panels:

    protected ObservableCollection<MyPanel> panels = new ObservableCollection<MyPanel>();
    private int counter = 0;
    

  4. Handle the Load event of the TextBox:

    private void txtText_Loaded(object sender, RoutedEventArgs e)
    {
        panels[counter].TextBox = (TextBox)sender;
        counter++;
    }
    

  5. If you want to access a particular TextBox, do it this way:

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

III) Add additional bindings for FontSize:

  1. Add a FontSize property to your MyPanel class:

    private double _fontSize = 10;
    public double FontSize
    {
        get { return _fontSize; }
        set
        {
            if (value != _fontSize)
            {
                _fontSize = value;
                NotifyPropertyChanged();
            }
        }
    }
    

  2. Bind just added property to the TextBox in your ItemsControl:

    <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}"
             FontSize="{Binding FontSize, Mode=OneWay}" />
    

  3. Add a slider to the template and bind it to the same property:

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

This way if you change the value on a slider, it will change the value in your MyPanel object bound to the panel. This in turn will change the font size of the textbox.

My whole code I tested it on looks like that:

<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>

And code behind:

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));
        }
    }
}

I personally would go with the last solution.
But again, let me know what libraries you are using, and I will have look at them when I have some time. Good luck.

这篇关于ItemsControl的文本框名称不工作在cs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆