English 中文(简体)
WPF 用户控制,文本Box Property 不发射的变化事件
原标题:WPF Usercontrol, TextBox PropertyChanged event not firing

我的用户控制有<条码> 排位/代码>,附在<条码>上。

我已询问该数据库,并对用户控制的答复加以约束,并展示正确的价值。

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.

<>User Control:

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

<>User Control.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 ofuser Control:

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

我预先表示歉意,我对世界森林论坛来说是新的{相对的}。 谁能看到我会错过这种情况?

我已成功地获得了我其他具有约束力的用户控制(该代码几乎是一份准确的复印件),但对其的答复是<代码>ListBox甄选,因为这一用户控制对<代码>TextBox具有约束力。

Thanks in advance, Kohan.

最佳回答

你们没有将文字箱与回答财产挂钩。 你们所做的事情是,在贵方财产上更换了手稿,当做改动时,你人工确定文本框。

你们的法典应当看一看这样的情况。

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

这是文本箱与对类别条件问题(用户控制)的财产问题的答复之间具有约束力的。 每当用户控制上的变化回答财产时,文本箱将更新,如果你改动文本箱的案文,将更新答复财产。 根据这一守则,你可以撤销你的答复。 财产 改变的方法已不再是绝对必要的。 具有约束力的条款

问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签