【发布时间】:2018-01-09 08:29:46
【问题描述】:
我有问题。我为我的应用程序创建了 2 个控件:一个 BottomMenu 控件和一个 ResultBox。 ResultBox 包含在 BottomMenu 中,因此顺序如下:Page -> BottomMenu -> ResultBox。我在 ResultBox 中创建了一个名为 Result 的字符串类型的依赖属性。
public ResultBox()
{
this.InitializeComponent();
this.DataContext = this;
}
/// <summary>
/// Property used to store the result of the calculation
/// </summary>
public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(
"Result",
typeof(string),
typeof(ResultBox),
new PropertyMetadata(null)
);
/// <summary>
/// String holding the text assigned to the Result
/// </summary>
public string Result
{
get => (string)GetValue(ResultProperty);
set => SetValue(ResultProperty, value);
}
绑定如下:
<TextBlock Style="{StaticResource DefaultTextBlockStyle}"
Text="{Binding Result}"/>
然后我在底部菜单中创建了相同的依赖属性,以便可以直接从页面设置。
public BottomMenu()
{
this.InitializeComponent();
this.DataContext = this;
}
/// <summary>
/// Property used to store the result of the calculation
/// </summary>
public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(
"Result",
typeof(string),
typeof(BottomMenu),
new PropertyMetadata(null)
);
/// <summary>
/// String holding the text assigned to the Result
/// </summary>
public string Result
{
get => (string)GetValue(ResultProperty);
set => SetValue(ResultProperty, value);
}
和绑定:
<local:ResultBox Grid.Row="1" Margin="0 0 0 10"
Result="{Binding Result}"
/>
不幸的是,如果我直接在 ResultBox 的声明中输入它,文本才会显示出来。当我进行双重绑定并在页面中输入时,
<local:BottomMenu Grid.Row="2"
Result="13"/>
它不起作用。我正在学习绑定,我想知道我在哪里做错了,或者这是否是做这件事的正确方法。
编辑:ResultBox 中的绑定不应包含 Source,现已修复。
【问题讨论】: