访问.Text& 。自定义控件中多个控件的内容

时间:2012-03-02 17:27:21

标签: c# wpf user-controls properties typeof

我创建了一个非常简单的UserControl,其中包含TextBoxComboBox

<StackPanel Orientation="Horizontal">        
    <MyNamespace:MultiBox Style="{StaticResource PhoneBoxStyle}" BoxType="Phone" Grid.Column="0" Grid.Row="0" Name="phoneNumber" Margin="50,0,5,5" MinWidth="250"/>
    <ComboBox Grid.Column="1" Grid.Row="0" Height="{Binding ElementName=phoneNumber, Path=Height}" MinWidth="100" Name="callResultsSelection" ItemsSource="{Binding Source={StaticResource callResults}}" Margin="0,0,5,5"/>
</StackPanel>

我需要能够导出.Text&amp;按下一个按钮的人的.SelectedItem值。我尝试使用如下所示的属性,但它似乎不起作用。它通过IntelliSense为控件公开.Text属性,但它不会按预期将任何内容复制到剪贴板。

原创(和期望)方法:

public string Text
    {
        get { return phoneNumber.Text + " - " + callResultsSelection.SelectedItem + "\r\n"; }
        set { value = phoneNumber.Text + " - " + callResultsSelection.SelectedItem + "\r\n"; }
    }

后备法:

public string Text
    {
        get { return phoneNumber.Text; }
        set { value = phoneNumber.Text; }
    }        

    public string ComboBoxSelection
    {
        get { return callResultsSelection.SelectedItem.ToString(); }
        set { value = callResultsSelection.SelectedItem.ToString(); }
    }

我正在使用的控制迭代如下。这些部分有很多,但这是唯一相关的部分。

foreach (object o in ccChildren.GetChildren(tool, 3))
            {
                if (o.GetType() == typeof(CallTemplate))
                {
                    CallTemplate template = (CallTemplate)o;
                    if (template.Text != null)
                    {
                        textBuffer += template.Text;
                    }
                    else
                    {
                        textBuffer = "";
                    }
                    tempString += textBuffer;
                    textBuffer = "";
                }
            }

通过使用断点,我知道它确实到达if块中的决策点,但即使VS识别CallTemplate对象,它也与它不匹配。有人看到问题吗?

编辑:我知道问题不在于迭代方法(ccChildren.GetChildren)。我使用它与许多其他控件(文本框,组合框,单选按钮,复选框),它工作得非常好。该区域唯一可能是问题的是CallTemplate类型。

2 个答案:

答案 0 :(得分:0)

这是用户控件xaml

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <StackPanel>
        <TextBox  Grid.Column="0" Grid.Row="0" Name="phoneNumber" Margin="50,0,5,5" MinWidth="250"/>
        <ComboBox Grid.Column="1" Grid.Row="0" Height="{Binding ElementName=phoneNumber, Path=Height}" MinWidth="100" Name="callResultsSelection" ItemsSource="{Binding stud}" DisplayMemberPath="Name" Margin="0,0,5,5"/>
    </StackPanel>

</Grid>

这是usercontrol的代码隐藏

using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            DataContext = this;
            stud = new ObservableCollection<Student>();
            stud.Add(new Student() { Name = " chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" });
            stud.Add(new Student() { Name = " chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" });
            stud.Add(new Student() { Name = "chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" });
            stud.Add(new Student() { Name = " chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" });
            stud.Add(new Student() { Name = "chauhan", RollNo = 1212, About = "dc wecwedc wec cwec wevcwe vcwd vcwvc" });
        }
        public ObservableCollection<Student> stud
        { get; set; }
        public string Text
        {
            get
            {
                return phoneNumber.Text + " - " + callResultsSelection.SelectedItem + "\r\n";
            }
        }
    }
    public class Student
    {
        public string Name { get; set; }
        public int RollNo { get; set; }
        public string About { get; set; }
    }
}

这是使用上述usercontrol的窗口xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:uc="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <uc:UserControl1 Grid.Row="0" x:Name="ucw"/>
        <Button Click="Button_Click" Grid.Row="1"/>
    </Grid>
</Window>

以下是窗口背后的代码

using System.Windows;
namespace WpfApplication1
{
    public partial class MainWindow : Window
    {    
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(ucw.Text);
        }
    }
}

这里当我点击按钮时,usercontrol的Text属性在消息框中给出了正确的值。希望这会有所帮助。

答案 1 :(得分:0)

所以这将是纯粹愚蠢的时刻之一。我唯一的问题是我没有将包含UserControl的GroupBox添加到GroupBoxes数组中以进行迭代。感谢有用的回复......