locked
Why property must be public, if it is used in xaml binding RRS feed

  • Question

  • There are thousands of public properties that are used for xaml binding in my application. I am trying to minimize publics for security reasons.  But looks like a property must be public if it is a source in xaml binding. If change it to internal, binding would not work anymore.

    why a INTERNAL property can't be binding source in xaml even they are in same assembly?

    for example

            internal User Owner
            {
                get { return this.owner; }
            }

    <TextBlock Name="OwnersNameDTKey" Grid.Row="4" Grid.Column="1"
                        Text="{Binding Path=Owner.Name}"

    an error will

    System.Windows.Data Error: 39 : BindingExpression path error: 'Owner' property not found on 'object' ''ListCollectionView' (HashCode=47980820)'. BindingExpression:Path=Owner.Name;


    • Edited by Shuming Xin Wednesday, October 17, 2012 2:53 AM
    Wednesday, October 17, 2012 2:52 AM

Answers

  • The properties you bind to have to be public...

    ...but the class they belong to doesn't!  :D

     

    <Window x:Class="WpfApplication5.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <Label Content="{Binding LabelContent}"/>
        </StackPanel>
    </Window>
     
    using System.Windows;
    using System.ComponentModel;
    
    namespace WpfApplication5
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new MyViewModel { LabelContent = "Hello" };
            }
    
            private class MyViewModel
            {
                public string LabelContent { get; set; }
            }
        }
    }
    

     

    MyViewModel is private, no-one will easily find it, or it's properties ;)

     

    Regards,
    Pete

     

    #PEJL

    • Proposed as answer by Mr. Javaman II Thursday, October 18, 2012 4:29 PM
    • Marked as answer by Shuming Xin Friday, October 19, 2012 5:38 AM
    Wednesday, October 17, 2012 10:01 PM

All replies

  • That's a good question, I wonder why as well.

    JP Cowboy Coders Unite!

    Wednesday, October 17, 2012 2:53 AM
  • Hi Shuming Xin,

    This is a required for WPF binding, you could refer to this MSDN document:

    http://msdn.microsoft.com/en-us/library/ms743643.aspx

    The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 17, 2012 11:07 AM
  • The properties you bind to have to be public...

    ...but the class they belong to doesn't!  :D

     

    <Window x:Class="WpfApplication5.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <Label Content="{Binding LabelContent}"/>
        </StackPanel>
    </Window>
     
    using System.Windows;
    using System.ComponentModel;
    
    namespace WpfApplication5
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new MyViewModel { LabelContent = "Hello" };
            }
    
            private class MyViewModel
            {
                public string LabelContent { get; set; }
            }
        }
    }
    

     

    MyViewModel is private, no-one will easily find it, or it's properties ;)

     

    Regards,
    Pete

     

    #PEJL

    • Proposed as answer by Mr. Javaman II Thursday, October 18, 2012 4:29 PM
    • Marked as answer by Shuming Xin Friday, October 19, 2012 5:38 AM
    Wednesday, October 17, 2012 10:01 PM
  • Pete;

      That's a great post!


    JP Cowboy Coders Unite!

    Thursday, October 18, 2012 4:29 PM