-1

I would like to display my score point in a textbox, even if the textbox.Text get the value, in GUI i can't see my score.

    <UserControl x:Class="WpfCyberPunk.UserControls.UserControlPlaying"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfCyberPunk.UserControls"
         mc:Ignorable="d" 
         d:DesignHeight="350" Width="Auto" d:DataContext="{d:DesignData }">
<Grid Name="PlayingGrid" Margin="0,0,0,0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>            
    </Grid.RowDefinitions>
    <Grid Row="0" HorizontalAlignment="Left">
        <StackPanel Name="SkPn_Score" Orientation="Horizontal">
            <TextBlock HorizontalAlignment="Left" Margin="10,1,0,0" TextWrapping="Wrap" Text="Point de départ" VerticalAlignment="Center" Height="25" Width="100"/>
            <TextBox x:Name = "tBx_StartPt" Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ScoreText,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" TextChanged="TBx_StartPt_OnTextChanged" FontSize="10" HorizontalAlignment="Left" Margin="10,1,0,0" TextWrapping="Wrap" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="25" Width="50"/>
        </StackPanel>
    </Grid>

IN cs, I have

 public partial class UserControlPlaying : UserControl
{
    private string _score;
    private int _point;

    //public event DependencyProperty PropertyChanged;

    public string Score
    {
        get => _score;
        set
        {
            _score = value;
            SetValue(ValueProperty, _score);
        }
    }
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Score", typeof(string), ownerType: typeof(UserControlPlaying), new PropertyMetadata(""));

And in my class, i have made an event to see, the new value

private void TBx_StartPt_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        string newScore = tBx_StartPt.Text;
    }

In debug, newScore get the new value.

I don't understand, anything does not display in GUI. Thank you for your help.

  • Take a look at the Output Window in Visual Studio when you debug the application. You will see a data binding error message that tells you that there is no ScoreText property in the UserControl. The Binding must use `Path=Score`. Besides that, your dependency property declaration is wrong. It must not use a backing field, but only call GetValue and SetValue. It must also adhere to a naming convention. See the answer to the duplicate question for how to declare it correctly. – Clemens Dec 03 '20 at 07:04

1 Answers1

0

You dependency property declaration is wrong. It must look like this:

public string Score
{
    get { return (string)GetValue(ScoreProperty); }
    set { SetValue(ScoreProperty, value); }
}

public static readonly DependencyProperty ScoreProperty =
    DependencyProperty.Register(
        nameof(Score),
        typeof(string),
        typeof(UserControlPlaying),
        new PropertyMetadata(""));

And fix the binding in the textbox:

<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
                        Path=Score,
                        Mode=TwoWay,
                        UpdateSourceTrigger=PropertyChanged}" .../>
Clemens
  • 123,504
  • 12
  • 155
  • 268
user2250152
  • 14,658
  • 4
  • 33
  • 57
  • Thank you for your answer. I have tried but I have the same trouble. My TextBox still does not display my string. – titanix Dec 04 '20 at 13:04
  • The question has been already closed by the moderator. Do you set UserControlPlaying.Score directly in code behind? If yes, please share your code how do you set it. Thanks – user2250152 Dec 04 '20 at 13:21