C# 在代码隐藏中访问silverlight用户控件的公共属性

C# 在代码隐藏中访问silverlight用户控件的公共属性,c#,wpf,silverlight,silverlight-5.0,C#,Wpf,Silverlight,Silverlight 5.0,我创建了一个简单的Silverlight用户控件,并定义了一个公共属性AllowMultiple public bool AllowMultiple{get;set;} 现在,我在XAML中设置此公共属性,如下所示: <Controls1:PeopleChooser Name="SinglePeopleChooser" AllowMultiple="False" Width="Auto" d:LayoutOverrides="Height"/> <Controls1:Peopl

我创建了一个简单的Silverlight用户控件,并定义了一个公共属性
AllowMultiple

public bool AllowMultiple{get;set;}

现在,我在XAML中设置此公共属性,如下所示:

<Controls1:PeopleChooser Name="SinglePeopleChooser" AllowMultiple="False" Width="Auto" d:LayoutOverrides="Height"/>
<Controls1:PeopleChooser Name="MultiplePeopleChooser" AllowMultiple="True" Width="Auto" d:LayoutOverrides="Height"/>

可能是因为在构造函数初始化期间,框架没有将此公共属性的值分配给对象。

使用backfield转换公共属性

    private bool _allowMultiple;
    public bool AllowMultiple
    {
        get { return _allowMultiple; }
        set { _allowMultiple = value; }
    }

在setter中放置一个断点并检查它是否命中构造函数,如果没有,您可以使用
Loaded
事件检查并利用该断点

我能够通过加载的事件解决它。不需要依赖属性。请参阅下面的代码。我可以在
Loaded
事件中成功访问属性值

 public PeopleChooser()
        {

            this.Loaded += PeopleChooser_Loaded;
            InitializeComponent();                     

        }


  void PeopleChooser_Loaded(object sender, RoutedEventArgs e)
        {
            if (AllowMultiple)
            {
                UsersListBox.Visibility = System.Windows.Visibility.Visible;
                UserTextBox.Visibility = System.Windows.Visibility.Collapsed;
                ResolveButton.Visibility = Visibility.Collapsed;            

            }
            else
            {
                UsersListBox.Visibility = System.Windows.Visibility.Collapsed;
                UserTextBox.Visibility = System.Windows.Visibility.Visible;
                ResolveButton.Visibility = Visibility.Visible;
            }

如果使用依赖项属性,则可以将其他元素属性绑定到人员选择器的AllowMultiple属性,并使用可见性转换器显示/隐藏它们。例如:

public partial class PeopleChooser : UserControl
{
    public PeopleChooser()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty AllowMultipleProperty = DependencyProperty.Register("AllowMultiple", typeof(bool), typeof(PeopleChooser), null);
    public bool AllowMultiple
    {
        get { return (bool)GetValue(AllowMultipleProperty); }
        set { SetValue(AllowMultipleProperty, value); }
    }
}

<UserControl x:Class="TestSilverlightApplication.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
    xmlns:lcl="clr-namespace:TestSilverlightApplication">
    <UserControl.Resources>
        <lcl:VisibilityConverter x:Key="VisibilityConverter" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Vertical">
            <Button Click="Button_Click" Content="Toggle allow multiple" />
            <lcl:PeopleChooser x:Name="lclPeopleChooser" AllowMultiple="False"></lcl:PeopleChooser>
            <TextBlock Text="Dependent content" Visibility="{Binding AllowMultiple, ElementName=lclPeopleChooser, Converter={StaticResource VisibilityConverter}}" />
        </StackPanel>

    </Grid>
</UserControl>

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        lclPeopleChooser.AllowMultiple = !lclPeopleChooser.AllowMultiple;
    }

public class VisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool parsedValue = false;
        bool.TryParse(value.ToString(), out parsedValue);
        if (parsedValue)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共部分类PeopleChooser:UserControl
{
公共人物选择器()
{
初始化组件();
}
公共静态只读DependencyProperty AllowMultipleProperty=DependencyProperty.Register(“AllowMultiple”、typeof(bool)、typeof(PeopleChooser)、null);
公共布尔允许多个
{
获取{return(bool)GetValue(AllowMultipleProperty);}
set{SetValue(AllowMultipleProperty,value);}
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
lclpeopechooser.AllowMultiple=!lclpeopechooser.AllowMultiple;
}
公共类可视性转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
bool parsedValue=false;
bool.TryParse(value.ToString(),out-parsedValue);
if(parsedValue)
{
返回可见性。可见;
}
返回可见性。折叠;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

通过这种方式,您可以避免页面事件,并可能将AllowMultiple属性绑定到视图模型属性,并让UI自行处理。

我认为它必须是依赖性属性。@TrueBlueAusie:是的,这没有什么不同,但目的是检查值何时设置。这样他就可以得到相应的值。此外,设置直接值不必是依赖项属性。对于绑定,它需要是依赖项属性。@TrueBlueAussie:您的注释权限中已经给出了解释。。谢谢你的否决票,并作了详细的解释……我在回答中已经提到了。。。是的,它实际上不需要dp。+1:为先前对DPs的混淆道歉。我正要发布同样的答案,但你自己发现了。
public partial class PeopleChooser : UserControl
{
    public PeopleChooser()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty AllowMultipleProperty = DependencyProperty.Register("AllowMultiple", typeof(bool), typeof(PeopleChooser), null);
    public bool AllowMultiple
    {
        get { return (bool)GetValue(AllowMultipleProperty); }
        set { SetValue(AllowMultipleProperty, value); }
    }
}

<UserControl x:Class="TestSilverlightApplication.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
    xmlns:lcl="clr-namespace:TestSilverlightApplication">
    <UserControl.Resources>
        <lcl:VisibilityConverter x:Key="VisibilityConverter" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Vertical">
            <Button Click="Button_Click" Content="Toggle allow multiple" />
            <lcl:PeopleChooser x:Name="lclPeopleChooser" AllowMultiple="False"></lcl:PeopleChooser>
            <TextBlock Text="Dependent content" Visibility="{Binding AllowMultiple, ElementName=lclPeopleChooser, Converter={StaticResource VisibilityConverter}}" />
        </StackPanel>

    </Grid>
</UserControl>

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        lclPeopleChooser.AllowMultiple = !lclPeopleChooser.AllowMultiple;
    }

public class VisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool parsedValue = false;
        bool.TryParse(value.ToString(), out parsedValue);
        if (parsedValue)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}