WPF TextBox不会更新绑定源

时间:2017-08-10 20:14:02

标签: wpf data-binding wpf-style


我有一个自定义样式的文本框,并绑定到它的属性。如果我更改了文本框属性中的值,则不会更改,但是如果属性从代码文本框更新更改为正确 我猜它的Mode = TwoWay在某个地方丢失,但我无法弄清楚我在哪里清除样式frm xaml它工作正常。
表格xaml:

<TextBox x:Name="tbFrames" Height="27" Canvas.Left="53" TextWrapping="Wrap" Text="{Binding Path=frames, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" Canvas.Top="185" Width="64" Style="{DynamicResource StyleTextBox}" PreviewTextInput="AnimationValidationTextBox"/>

样式xaml:

<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="Height" Value="27" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <Border
                        x:Name="PART_border"
                        Background="Transparent"
                        BorderBrush="#FF434346"
                        BorderThickness="1"
                        />
                    <TextBox Foreground="#FFBABAC7" BorderThickness="0" Background="Transparent" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay}"  HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="PART_border" Property="Background" Value="#FF414146" />
                        <Setter TargetName="PART_border" Property="BorderBrush" Value="#FF5A5A5D" />
                        <Setter TargetName="PART_border" Property="Opacity" Value="0.7" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

实施INotifyPropertyChanged:

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

如果我从控制源updetes中移除我的风格,看起来像我之前所说的那样工作 首先,样式文本框绑定是:

<TextBox Foreground="#FFBABAC7" BorderThickness="0" Background="Transparent" Text="{TemplateBinding Text}"  HorizontalAlignment="Center" VerticalAlignment="Center"/>

它没有用,所以我把它改成了RelativeSource和TwoWay模式,但仍然没有结果。
可能有什么不对?

PreviewTextInputCode是:

private void AnimationValidationTextBox(object sender, TextCompositionEventArgs e)
{
    if (!animationOpened)
    {
        e.Handled = true;
        return;
    }

    NumberValidationTextBox(sender, e);
}

private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
    Regex regex = new Regex("[^0-9]+");
    e.Handled = regex.IsMatch(e.Text);
}

属性声明为:

   private int _frames;
    public int frames
    {
        get => _frames;
        set
        {
            _frames = value;
            OnPropertyChanged("frames");
        }
    }

默认样式框架都可以通过所有这些检查进行更新。

0 个答案:

没有答案
相关问题