在后面的代码中访问silverlight用户控件的公共属性

时间:2014-01-26 21:05:28

标签: 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:PeopleChooser Name="MultiplePeopleChooser" AllowMultiple="True" Width="Auto" d:LayoutOverrides="Height"/>

我想知道,这是我能获得这个公共财产价值的最佳事件。我试图在构造函数中获取此值,并尝试隐藏/显示我的用户控件中的一些控件,但它不起作用。

public PeopleChooser()
{
    InitializeComponent();            

    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;
    }

}

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

3 个答案:

答案 0 :(得分:1)

使用后场转换公共属性

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

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

答案 1 :(得分:1)

我能够通过加载的事件来解决它。不需要依赖属性。请参阅下面的代码。我可以在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;
            }

答案 2 :(得分:1)

如果使用依赖项属性,则可以将其他元素属性绑定到人员选择器的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();
    }
}

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