WPF Usercontrol,TextBox PropertyChanged事件未触发

时间:2010-01-18 16:02:38

标签: wpf data-binding user-controls listbox dependency-properties

我的用户控件DependencyProperty Answer附加到TextBox

我查询了数据库,并将答案绑定到usercontrol,并显示正确的值。

编辑TextBox时出现问题,PropertyChanged事件未触发,从而阻止我将正确的值保存回数据库。 请参阅下面的代码。

用户控件:

<Grid>
    <StackPanel>
            <TextBlock Name="txtbQuestion" TextWrapping="Wrap"  Text="Question" Foreground="Black" Margin="5"  Style="{DynamicResource Label}" ToolTip="{Binding  RelativeSource={RelativeSource Self}, Path=Text}" ></TextBlock>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition  Width="100" />
            </Grid.ColumnDefinitions>
            <TextBox Name="txtAnswer" Margin="5" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" >
                <TextBox.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=cbMultiLine, Path=IsChecked}" Value="True">
                                <Setter Property="TextBox.TextWrapping" Value="Wrap" />
                                <Setter Property="TextBox.Height" Value="100" />
                                <Setter Property="TextBox.AcceptsReturn" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>
            <CheckBox Name="cbMultiLine" Content="MultiLine" Margin="5" FontFamily="Georgia" Grid.Column="1" />
        </Grid>
        <Line Fill="Black" Margin="4" />
    </StackPanel>
</Grid>

Usercontrol.cs:

public partial class ConditionQuestion : UserControl
{

    public static readonly DependencyProperty AnswerProperty =
DependencyProperty.Register("Answer", typeof(string), typeof(ConditionQuestion), new UIPropertyMetadata(null, Answer_PropertyChanged));

    public static readonly DependencyProperty QuestionProperty =
        DependencyProperty.Register("Question", typeof(string), typeof(ConditionQuestion), new UIPropertyMetadata(null, Question_PropertyChanged));

    public ConditionQuestion()
    {
        InitializeComponent();
    }

    public string Answer
    {
        get { return (string)GetValue(AnswerProperty); }
        set { SetValue(AnswerProperty, value); }
    }

    public string Question
    {
        get { return (string)GetValue(QuestionProperty); }
        set { SetValue(QuestionProperty, value); }
    }

    private static void Answer_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        ((ConditionQuestion)source).txtAnswer.Text = (string)e.NewValue;
    }

    private static void Question_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        ((ConditionQuestion)source).txtbQuestion.Text = (string)e.NewValue;
    }

}

声明Usercontrol的实例:

<ListBox ItemContainerStyle="{StaticResource noSelect}" ItemsSource="{Binding Answer}"
         Name="lbQuestions" BorderBrush="#E6E6E6" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <my:ConditionQuestion Question="{Binding ConditionReportFormQuestions.Question}"
                                  Answer="{Binding Answer, Mode=TwoWay,
                                           UpdateSourceTrigger=PropertyChanged}"  />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我提前道歉,我是WPF的新手。谁能看到我可能出错的地方?

我已成功获得其他用户控件的绑定和更新(此代码几乎是一个完整的副本),但它们的答案是ListBox选择,其中 - 此usercontrol绑定到TextBox。< / p>

提前致谢, 钢钣。

1 个答案:

答案 0 :(得分:4)

您尚未将文本框绑定到answer属性。你所做的是在你的答案属性上放置一个更改的处理程序,当它被更改时你手动设置文本框文本属性。

您的代码看起来应该是这样的

<TextBlock
   Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:ConditionQuestion}}, Path=Answer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

这是文本框与属性AnswerQuestion(用户控件)类之间的绑定。每当用户控件上的Answer属性发生更改时,文本框都将更新,每当您更改文本框中的文本时,Answer属性都将更新。使用此代码,您可以删除不再需要的Answer_PropertyChanged方法。绑定处理它