【发布时间】:2012-03-02 17:27:21
【问题描述】:
我创建了一个相当简单的UserControl,由TextBox 和ComboBox 组成。
<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 和.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 类型。
【问题讨论】:
-
在属性 .Text @ set{} 中你应该使用值,而不是赋值!
-
else { textBuffer = ""; } 好像错了?!
-
我将值切换到等号的另一边,没有效果。至于 textBuffer = "",它所遍历的控件是一系列标签和文本框。在 if 块之前,将标签添加到 textBuffer。但是如果标签所指的控件为空,则标签不需要添加到 tempString 中,因此没有空行。
-
调试的时候这个是真还是假(template.Text != null)
-
刚刚又查了一下,都不是。似乎问题一定出在它检查类型上,因为它从来没有设法让它进入那个 if 块。但是,如果 VS 将其识别为有效类型,那又如何呢?
标签: c# wpf user-controls properties typeof