双向绑定使用用户控件...绑定到对象,而不是元素?

时间:2010-05-24 02:20:01

标签: wpf binding binding-mode

我创建了一个带有默认值的简单属性的对象。然后我创建了一个用户控件,其中包含一个文本框。我将用户控件的datacontext设置为对象。

文本框正确显示属性默认值,但是当用户更改文本框值时,我似乎无法更新属性值。我创建了一个简单的项目来说明我的代码。

感谢您的帮助!!

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private string _titleValue;

        public string TitleValue
        {
            get
            {
                return _titleValue;
            }
            set 
            {
                _titleValue = value;
                textBox1.Text = _titleValue;
            }
        }

        public static readonly DependencyProperty TitleValueProperty = DependencyProperty.Register(
           "TitleValue", typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata(new PropertyChangedCallback(titleUpdated))
           );

//Don't think I should need to do this!!!
        private static void titleUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((UserControl1)d).TitleValue = (string)e.NewValue; 
        }
    }

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="94,97,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"
                  Text="{Binding Path=TitleValue, Mode=TwoWay}"/>
    </Grid>
</UserControl>

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                var dummy = new DummyObject("This is my title.");
                userControl11.DataContext = dummy;

            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("The value is: " + ((DummyObject)userControl11.DataContext).Title);
            }
        }

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
        <Grid>
            <my:UserControl1 HorizontalAlignment="Left" Margin="95,44,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="191" Width="293" 
                             TitleValue="{Binding Path=Title, Mode=TwoWay}"/>
            <Button Content="Check Value" Height="23" HorizontalAlignment="Left" Margin="20,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </Grid>
    </Window>

1 个答案:

答案 0 :(得分:2)

未设置usercontrol上的DataContext。为它指定Name(我通常称之为“ThisControl”)并修改TextBox与Text="{Binding ElementName=ThisControl, Path=TitleValue, Mode=TwoWay}"的绑定。您也可以明确设置DataContext,但我相信这是首选方式。

似乎默认的DataContext应该是“this”,但默认情况下,它什么都没有。

[edit]您可能还想将, UpdateSourceTrigger=PropertyChanged添加到绑定中,因为默认情况下TextBoxes的Text绑定仅在焦点丢失时更新。