【发布时间】:2019-07-25 12:32:28
【问题描述】:
WPF 相当初级 :)
上下文
我有一个包含几个整数的 DataContext 窗口。 我的窗口基本上只是用来显示每个 int 的“Key : Value”,Value 是一个可编辑的 TextBox。
当我编辑我的窗口以提供 ValueConverters 和 ValidationRules 时,我觉得我可以使用一些通用类,因为它们都有相同的规则/转换器。
问题
假设我有 int property_a, int property_b, int property_c;在我的数据上下文中。我不知道如何将泛型类中 TextBox 的内容绑定到这些属性:/
我想为我的泛型类提供一个像“property_a”这样的字符串属性,但是我找不到如何使用这个属性进行动态绑定。
代码示例
- 我自定义的 TextBox 包装器:
<Grid>
<TextBox x:Name="IntBox" Grid.Row="1" Grid.Column="1" Margin="0, 5, 10, 5"
Style="{StaticResource TextBoxInError}">
<TextBox.Text>
<Binding Path="{no idea what to put here }">
<Binding.ValidationRules>
<validation:IntRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
public partial class IntTextBox : UserControl
{
public string PropertyToBind { get; set; }
public IntTextBox()
{
InitializeComponent();
}
}
- 我在 Window 中使用 TextBox 包装器的地方:
<input:IntTextBox x:Name="InitiativeBox" Grid.Row="1" Grid.Column="1" Margin="0, 5, 10, PropertyToBind="Initiative"/>
感谢您的阅读:)
【问题讨论】:
-
请不要在您的帖子中添加“已解决”。如果答案对你有用,只要接受就足够了。