2

I have a usercontrol with a DependencyProperty of Answer which is attached to a TextBox.

I have queried the database and bound the answer to the usercontrol and the correct value is displayed.

The issue occurs when I edit the TextBox, the PropertyChanged event is not firing and thus preventing me from saving the correct value back to the database. Please see below my code.

Usercontrol:

<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;
    }

}

Declaring instance of 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>

I apologize in advance, I am {relatively} new to WPF. Can anyone see where I may be going wrong with this?

I have successfully got my other usercontrols binding and updating (this code is almost an exact copy) but the answers on them are ListBox selections where-as this usercontrol is binding to a TextBox.

Thanks in advance, Kohan.

3
  • Sorry, maybe I'm dumb at all but I can't see where the txtAnswer text box is bound to the Answer property..
    – levanovd
    Jan 18, 2010 at 16:37
  • my:ConditionQuestion has a property called Answer, this is a DependencyProperty that i created. When this property changes it updates the textbox within the usercontrol.
    – 4imble
    Jan 18, 2010 at 21:05
  • But where is feedback from the text box?
    – levanovd
    Jan 19, 2010 at 15:38

1 Answer 1

4

You have not bound the text box to the answer property. What you have done is put a changed handler on your answer property and when it is changed you manually set the text boxes text property.

Your code should look something like this

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

this is a binding between the textbox and the property Answer on the class ConditionQuestion (the user control). Whenever the Answer property changes on the user control the text box will update and whenever you change the text in the textbox the Answer property will be updated. With this code you can remove your Answer_PropertyChanged method it is no longer neccessary. the binding takes care of it

3
  • 1
    Works perfectly, i love you man. I don't fully understand it at the moment but i will look at it more later when i get some spare time. Thanks again, take care.
    – 4imble
    Jan 19, 2010 at 9:30
  • binding means never having to update your user interface from your object model and never having to update your object model from your user interface. the binding glues the two together, when the model changes the user interface reflects the changes, when the user interface changes the model gets updated. once you understand the concept you can design your user interface and model independently of each other. Jan 19, 2010 at 10:57
  • Wow, my idea was right, but I thought it is too easy for an answer and I wrote it in comments ))
    – levanovd
    Jan 19, 2010 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.