Search code examples
c#wpfpropertychanged

Wpf PropertyChanged two time to two variable in one method with button send


I cant update variable. I have a User control class with custom textbox. WPF UC code.

<TextBox x:Name="wwwBox" TextChanged="WwwBoxTextChanged" Width="{Binding BoxWidth}" Text="{Binding LinkAddress,Mode=TwoWay}"/>

I was trying With Mode=TwoWay and without it. WPF UC class code.

public partial class TextBoxWebControl : UserControl, INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;
        public int BoxWidth { get; set; }
        public bool IsValid { get; set; }
        public string LinkAddress { get; set; }
        public TextBoxWebControl()
        {
            InitializeComponent();
            DataContext = this;
            IsValid = false;
        }
        public void WwwBoxTextChanged(object sender, TextChangedEventArgs args)
        {
            Uri uriResult;
            IsValid = Uri.TryCreate(wwwBox.Text, UriKind.Absolute, out uriResult)
                && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(IsValid)));
        }
}

When i adding second PropertyChanged to link address i cant write anything to textbox. Next i need to send LinkAddres(content of textbox called wwwBox) via button. Here is a MainWindow WPF Code

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1">
            <Label>Enter the website link..</Label>
        </StackPanel>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="2">
            <uc:TextBoxWebControl x:Name="wwwLink" BoxWidth="500" />
        </StackPanel>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="3">
            <Button Click="GetWebSiteDetail"  Tag="{ Binding Path= LinkAddress, ElementName=wwwLink,UpdateSourceTrigger=PropertyChanged}" Content="Get all!"  Width="100" Visibility="{Binding Path= IsValid, ElementName=wwwLink,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource VisibleIfTrueConverter }}"/>
        </StackPanel>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="4">
            <Label Content="Bad address" >
                <Label.Style>
                    <Style TargetType="{x:Type Label}">
                        <Setter Property="Visibility" Value="Hidden"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path= IsValid,ElementName=wwwLink,UpdateSourceTrigger=PropertyChanged}" Value="True">
                                <Setter Property="Visibility" Value="Hidden"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path= IsValid,ElementName=wwwLink,UpdateSourceTrigger=PropertyChanged}" Value="False">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Label.Style>
            </Label>
        </StackPanel>

And Main window WPF class Code

private void GetWebSiteDetail(object sender, RoutedEventArgs e)
{
    string link = new WebDetail(((Button)sender).Tag.ToString());    //here always null   
}

Is my first wpf project. Glad for any advice how to solve my problem and how i can upgrade my code.


Solution

  • You need to raise the PropertyChanged event for the data bound LinkAddress property for the Tag property to get updated:

    private string _linkAddress;
    public string LinkAddress
    {
        get { return _linkAddress; }
        set { _linkAddress = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(LinkAddress))); }
    }
    

    Also, there is no need to handle the TextChanged event for a data bound property. You might as well move your code from WwwBoxTextChanged to the setter of LinkAddress.