How to solve error The member "xxx" is not recognized or is not accessible

Sarah 186 Reputation points
2022-10-12T07:38:14.17+00:00

I have created the following style but I get the error: "The member "WatterMarkText" is not recognized or is not accessible".
Can anyone help to fix the error?

 **<Style x:Key="PasswordWatermark" BasedOn="{StaticResource {x:Type PasswordBox}}" TargetType="{x:Type PasswordBox}">  
        <Setter Property="Width" Value="250"/>  
        <Setter Property="Height" Value="25"/>  
        <Setter Property="Margin" Value="10"/>  
        <Setter Property="PWB:PasswordBoxMonitor.IsMonitoring" Value="True"/>  
        <Setter Property="Template">  
            <Setter.Value>  
                <ControlTemplate TargetType="{x:Type PasswordBox}">  
                    <Border Name="Bd" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true">  
                        <Grid>  
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>  
                            <TextBlock Text="{TemplateBinding WatterMarkText}"   
                                       Style="{StaticResource PwBoxWatterMarkTextStyle}"   
                                       Name="txtPrompt"/>  
                        </Grid>  
                    </Border>  
                    <ControlTemplate.Triggers>  
                        <Trigger Property="IsEnabled" Value="false">  
                            <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>  
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>  
                        </Trigger>  
                        <Trigger Property="PWB:PasswordBoxMonitor.PasswordLength" Value="0">  
                            <Setter Property="Visibility" TargetName="txtPrompt" Value="Visible"/>  
                        </Trigger>  
                    </ControlTemplate.Triggers>  
                </ControlTemplate>  
            </Setter.Value>  
        </Setter>  
    </Style>**  
Developer technologies | Windows Presentation Foundation
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-10-13T07:42:55.663+00:00

    Hi,@Sarah . If you want to use code like below, you need to add DependencyProperty WatterMarkText to PasswordBox.

    <PasswordBox WatterMarkText ="Hello World" />  
    

    A common approach is to add Attached Dependency Property to the project.

     public class Helper  
        {  
            public static string GetWatterMarkText(DependencyObject obj)  
            {  
                return (string)obj.GetValue(WatterMarkTextProperty);  
            }  
      
            public static void SetWatterMarkText(DependencyObject obj, string value)  
            {  
                obj.SetValue(WatterMarkTextProperty, value);  
            }  
            public static readonly DependencyProperty WatterMarkTextProperty =  
               DependencyProperty.RegisterAttached("WatterMarkText", typeof(string), typeof(Helper),  
                   new FrameworkPropertyMetadata(" ", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));  
        }  
    

    Update your code as follows.

      <TextBox  Text="{Binding Path=(local:Helper.WatterMarkText), RelativeSource={RelativeSource TemplatedParent}}"  
                                               Style="{StaticResource PwBoxWatterMarkTextStyle}"  Name="txtPrompt">  
    

    Styles added for testing.
    249946-image.png

    Password :

     <Grid>  
            <PasswordBox Width="100" Height="50" local:Helper.WatterMarkText="Hello World" Style="{StaticResource PasswordWatermark}"/>  
        </Grid>  
    

    The result:
    249910-image.png
    By the way, you can pay attention to the difference between Template Binding and Binding.

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.