C
C#4mo ago
quantum

WPF TextBlock Text binding

Hello i am working with wpf for very first time. I am trying to make a textbox with placeholder but when i try to use this resource it says The member "Placeholder" is not recognized or is not accessible. Here is my code
PlaceholderTextbox.xaml
<Grid>
<TextBlock
x:Name="txtPlaceholder"
VerticalAlignment="Center"
HorizontalAlignment="Left"
FontSize="16"
Padding="10,0,0,0"
Foreground="#a3a3a4"
Text="{Binding Placeholder}"
/>
</Grid>
PlaceholderTextbox.xaml
<Grid>
<TextBlock
x:Name="txtPlaceholder"
VerticalAlignment="Center"
HorizontalAlignment="Left"
FontSize="16"
Padding="10,0,0,0"
Foreground="#a3a3a4"
Text="{Binding Placeholder}"
/>
</Grid>
PlaceholderTextbox.xaml.cs
public PlaceholderTextbox()
{
InitializeComponent();
DataContext = new CustomTextboxViewmodel();
}
PlaceholderTextbox.xaml.cs
public PlaceholderTextbox()
{
InitializeComponent();
DataContext = new CustomTextboxViewmodel();
}
PlaceholderTextboxVM.cs
class CustomTextboxViewmodel : Base
{
private string placeholder = "Placeholder";

public string Placeholder
{
get { return placeholder; }
set { placeholder = value; OnPropertyChanged(); }
}
}
PlaceholderTextboxVM.cs
class CustomTextboxViewmodel : Base
{
private string placeholder = "Placeholder";

public string Placeholder
{
get { return placeholder; }
set { placeholder = value; OnPropertyChanged(); }
}
}
13 Replies
constantine
constantine4mo ago
Try to make your view model class public
quantum
quantum4mo ago
Yeah i have tried that but still same
constantine
constantine4mo ago
When are you getting the mentioned error message? Upon the compilation? Because usually even if there are binding issues, it still compiles and runs.
quantum
quantum4mo ago
Yeah i got that error on compilation
constantine
constantine4mo ago
Did you try to run “clean solution”?
quantum
quantum4mo ago
No i didnt let me try now it is same and more
quantum
quantum4mo ago
No description
quantum
quantum4mo ago
I have removed bin and obj folder and now my errors are these only
No description
constantine
constantine4mo ago
You see now? It looks for the Placeholder property in the TextboxPlaceholder class, not in the view model. This means that data context is not set properly. Try to put a breakpoint at the constructor of the TextboxPlaceholder. Does it been called? Also it could be helpful if you share your solution on GitHub or somewhere, the devil may hide in other details.
quantum
quantum4mo ago
Actually i have changed the viewmodel class name while i am trying solutions at the end i couldnt find any solutions and i have completely removed the viewmodel class i am doing things in directly on PlaceholderTextbox.xaml.cs now it works for sure and i have lost more than one day on here for looking solutions so i stopped looking for solution
quantum
quantum4mo ago
No description
quantum
quantum4mo ago
No description
quantum
quantum4mo ago
PlaceholderTextboxVM.cs
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApp2.ViewModels
{
internal class PlaceholderTextboxVM : INotifyPropertyChanged
{
#region Properties
private string placeholder = "Placeholder";

public string Placeholder
{
get { return placeholder; }
set { placeholder = value; OnPropertyChanged(); }
}

private string text = "";

public string Text
{
get { return text; }
set { text = value; OnPropertyChanged(); }
}

private bool isclearable = false;

public bool IsClearable
{
get { return isclearable; }
set { isclearable = value; OnPropertyChanged(); }
}

private bool ispassword;

public bool IsPassword
{
get { return ispassword; }
set { ispassword = value; OnPropertyChanged(); }
}
#endregion

public event PropertyChangedEventHandler? PropertyChanged = null;

protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApp2.ViewModels
{
internal class PlaceholderTextboxVM : INotifyPropertyChanged
{
#region Properties
private string placeholder = "Placeholder";

public string Placeholder
{
get { return placeholder; }
set { placeholder = value; OnPropertyChanged(); }
}

private string text = "";

public string Text
{
get { return text; }
set { text = value; OnPropertyChanged(); }
}

private bool isclearable = false;

public bool IsClearable
{
get { return isclearable; }
set { isclearable = value; OnPropertyChanged(); }
}

private bool ispassword;

public bool IsPassword
{
get { return ispassword; }
set { ispassword = value; OnPropertyChanged(); }
}
#endregion

public event PropertyChangedEventHandler? PropertyChanged = null;

protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
you can check these if you want but it still same i cannot build while i am using this properties
Want results from more Discord servers?
Add your server