WPF usercontrol属性更改不会触发事件?

时间:2018-06-15 02:33:25

标签: wpf data-binding user-controls

我为我的wpf项目写了一个 Usercontrol 。这是一个 IPAddress 控件,其中包含四个 TextBox ,如下所示:

 <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
    </Grid.ColumnDefinitions>
    <TextBox  Grid.Column="0"  x:Name="txtboxFirstPart" TextChanged="txtbox_TextChanged" MaxLength="3" BorderThickness="0" TabIndex="1" PreviewKeyDown="txtboxFirstPart_PreviewKeyDown" PreviewKeyUp="txtboxFirstPart_PreviewKeyUp" Width="31" />
    <Label Content="." Grid.Column="1"   x:Name="label1" VerticalContentAlignment="Bottom" HorizontalAlignment="Left" Background="White" />
    <TextBox Grid.Column="2" x:Name="txtboxSecondPart" MaxLength="3" TextChanged="txtbox_TextChanged" BorderThickness="0" TabIndex="2" KeyDown="txtboxSecondPart_KeyDown" PreviewKeyUp="txtboxSecondPart_PreviewKeyUp" Margin="0" HorizontalAlignment="Left" Width="31"/>
    <Label Content="." Grid.Column="3" Grid.Row="0" x:Name="label2" VerticalContentAlignment="Bottom" HorizontalAlignment="Left" Background="White" />
    <TextBox  Grid.Column="4" x:Name="txtboxThridPart" MaxLength="3" TextChanged="txtbox_TextChanged" BorderThickness="0" TabIndex="3" VerticalContentAlignment="Bottom"  KeyDown="txtboxThridPart_KeyDown" PreviewKeyUp="txtboxThridPart_PreviewKeyUp" Width="31" />
    <Label Content="." Grid.Column="5" x:Name="label3" VerticalContentAlignment="Bottom" HorizontalAlignment="Left" Background="White" />
    <TextBox  Grid.Column="6" x:Name="txtboxFourthPart" MaxLength="3" TextChanged="txtbox_TextChanged" BorderThickness="0" TabIndex="4" VerticalContentAlignment="Bottom" HorizontalAlignment="Left" Width="31" HorizontalContentAlignment="Center" />
</Grid>

我将文字属性添加到其中:

public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(IPAddressControl),
                                                             new PropertyMetadata("0.0.0.0", new PropertyChangedCallback(OnTextPropertyChanged)));
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    IPAddressControl o = d as IPAddressControl;
    try
    {
        var value = e.NewValue as string;
        var splitValues = new string[4];
        if (value != null)
        {
            var splits = value.Split(new char[] { '.' }, StringSplitOptions.None);
            Array.Copy(splits, splitValues, splits.Length);
        }
        o.txtboxFirstPart.Text = splitValues[0];
        o.txtboxSecondPart.Text = splitValues[1];
        o.txtboxThridPart.Text = splitValues[2];
        o.txtboxFourthPart.Text = splitValues[3];
    }
    catch (Exception ex)
    {
        throw new Exception("Error in IP control see inner exception!", ex);
    }
    o.RaiseTextChanged(e);
}

public string Text
{
    get { return (string)this.GetValue(IPAddressControl.TextProperty); }
    set { this.SetValue(IPAddressControl.TextProperty, value); }
}

public event DependencyPropertyChangedEventHandler TextChanged;
protected virtual void RaiseTextChanged(DependencyPropertyChangedEventArgs e)
{
    if (this.TextChanged != null)
    {
        this.TextChanged(this, e);
    }
}

当我在像我这样的数据绑定项目中使用它时:

<GroupBox Name="gb_Tcp" DataContext="{Binding TcpCollection}">
   <my:IPAddressControl Text="{Binding IpAddress}" />   
</GroupBox>

我成功阅读了绑定源中的 IpAddress 。现在我更改了此用户控件的值,并在button_click事件中尝试将此用户控件的值重置为默认值,如下所示:

private void btn_IpReset_Click(object sender, RoutedEventArgs e)
{
    tb_IpAddress.Text = "...";
}

我发现 tb_IpAddress.Text 值是默认值,但 txtboxFirstPart (我更改的用户控件的一个文本框)保留旧值。这表明没有触发 DepedencyProperty OnTextPropertyChanged。

那么我应该如何解决这个问题呢?提前谢谢!

0 个答案:

没有答案