views:

439

answers:

1

I have a usercontrol with the Dependancy Property 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 fireing 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.

+2  A: 

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

Aran Mulholland
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.
Kohan
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.
Aran Mulholland
Wow, my idea was right, but I thought it is too easy for an answer and I wrote it in comments ))
levanovd