如何强制从 TemplateControl 更新数据源

发布于 2024-12-22 11:48:59 字数 2014 浏览 1 评论 0原文

解决一个简单的问题让我伤透了脑筋。我有一个带有填充模板属性的自定义控件。该模板是一个简单的网格,里面有一个文本框。该文本框通过 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_UpdateControlbutton1_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-12-29 11:48:59

不应手动重新调用绑定。相反,您应该尝试使当前的绑定机制正常工作。
您的 TextBox 应绑定到 DependancyProperty 或 TextBox 的 DataContext 应实现 < a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" rel="nofollow">INotifyPropertyChanged。
像这样定义您的 Value2 属性:

public static readonly DependencyProperty Value2Property = 
    DependencyProperty.Register("Value2", typeof(string), typeof(MyClass), new PropertyMetadata(string.Empty));

public string Value2
{
    get { return (string)GetValue(Value2Property); }
    set { SetValue(Value2Property, value); }
}

此外,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:

public static readonly DependencyProperty Value2Property = 
    DependencyProperty.Register("Value2", typeof(string), typeof(MyClass), new PropertyMetadata(string.Empty));

public string Value2
{
    get { return (string)GetValue(Value2Property); }
    set { SetValue(Value2Property, value); }
}

Also, MyClass should implement UIElement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文