如何强制从 TemplateControl 更新数据源
解决一个简单的问题让我伤透了脑筋。我有一个带有填充模板属性的自定义控件。该模板是一个简单的网格,里面有一个文本框。该文本框通过 setter 和 getter 绑定到单例属性。如何以编程方式强制 TextBox 从单例中读取值并将其放回?
<Window x:Class="Spike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="305" Width="521" xmlns:my="clr-namespace:Spike" xmlns:Data="clr-namespace:Spike.Data">
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="editingTemplate">
<Grid>
<TextBox Text="{Binding Source={x:Static Data:MyClass.Instance}, Path=Value2}"/>
</Grid>
</ControlTemplate>
</Grid.Resources>
<UserControl Template="{StaticResource editingTemplate}" HorizontalAlignment="Left" Margin="58,60,0,0" x:Name="myUserControl1" VerticalAlignment="Top" Height="75" Width="284" />
<Button Content="Update source" Height="23" HorizontalAlignment="Left" Margin="184,23,0,0" Name="button1" VerticalAlignment="Top" Width="111" Click="button1_UpdateSource" Focusable="False" />
<Button Content="Update control" Focusable="False" Height="23" HorizontalAlignment="Left" Margin="58,23,0,0" Name="button2" VerticalAlignment="Top" Width="111" Click="button2_UpdateControl" />
</Grid>
</Window>
namespace Spike.Data
{
public class MyClass
{
private static readonly MyClass MyClassInstance = new MyClass();
public MyClass()
{
Value1 = "value1";
Value2 = "value2";
}
public static MyClass Instance
{
get { return MyClassInstance; }
}
public string Value1 { get; set; }
public string Value2 { get; set; }
}
}
换句话说,在 button2_UpdateControl 和 button1_UpdateSource 方法中应该实现什么?
预先感谢您的帮助
I broke my head by solving a simple problem. I have a custom control with filled Template property. The template is a simple Grid with a TextBox inside. This text box is bound to a singleton's proprty with setter and getter. How can I programmatically force the TextBox to read value from the singleton and put it back?
<Window x:Class="Spike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="305" Width="521" xmlns:my="clr-namespace:Spike" xmlns:Data="clr-namespace:Spike.Data">
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="editingTemplate">
<Grid>
<TextBox Text="{Binding Source={x:Static Data:MyClass.Instance}, Path=Value2}"/>
</Grid>
</ControlTemplate>
</Grid.Resources>
<UserControl Template="{StaticResource editingTemplate}" HorizontalAlignment="Left" Margin="58,60,0,0" x:Name="myUserControl1" VerticalAlignment="Top" Height="75" Width="284" />
<Button Content="Update source" Height="23" HorizontalAlignment="Left" Margin="184,23,0,0" Name="button1" VerticalAlignment="Top" Width="111" Click="button1_UpdateSource" Focusable="False" />
<Button Content="Update control" Focusable="False" Height="23" HorizontalAlignment="Left" Margin="58,23,0,0" Name="button2" VerticalAlignment="Top" Width="111" Click="button2_UpdateControl" />
</Grid>
</Window>
namespace Spike.Data
{
public class MyClass
{
private static readonly MyClass MyClassInstance = new MyClass();
public MyClass()
{
Value1 = "value1";
Value2 = "value2";
}
public static MyClass Instance
{
get { return MyClassInstance; }
}
public string Value1 { get; set; }
public string Value2 { get; set; }
}
}
In other words what should be implemented in button2_UpdateControl and button1_UpdateSource methods?
Thank you in advance for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不应手动重新调用绑定。相反,您应该尝试使当前的绑定机制正常工作。
您的 TextBox 应绑定到 DependancyProperty 或 TextBox 的 DataContext 应实现 < a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" rel="nofollow">INotifyPropertyChanged。
像这样定义您的
Value2
属性:此外,MyClass 应该实现 UIElement。
A Binding shouldn't be re-invoked manually. Instead, you should try and make your current Binding mechanism work properly.
Your TextBox should bind to DependancyProperty or the DataContext of the TextBox should implement INotifyPropertyChanged.
Define your
Value2
property like this:Also, MyClass should implement UIElement.