寻址绑定问题的子类中的属性

时间:2012-11-02 14:38:10

标签: c# windows-phone-7 binding

我有问题,我不确定它是什么。我在类中有一个类,它有一个需要绑定到控件的值,在本例中是可见性。代码正在正确地改变值,但输出没有改变(即折叠控件)

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel Orientation="Vertical">
            <Button x:Name="buttonOne" Content="Show Hide" Width="Auto" Click="buttonOne_Click"/>

        <ListBox x:Name="aListBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                            <TextBlock x:Name="blockOne" Grid.Column="0" Text="Raw "/>
                            <TextBlock x:Name="blockTwo" Grid.Column="1" Text="{Binding aValue}" Visibility="{Binding Path=visControl.VisibleState, BindsDirectlyToSource=True}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        </StackPanel>

    </Grid>

公共部分类MainPage:PhoneApplicationPage     {

    private List<myClass> listOfClasses = new List<myClass>();
    // Constructor
    public MainPage()
    {

        myClass classA = new myClass("one");
        myClass classB = new myClass("two");
        myClass classC = new myClass("three");
        listOfClasses.Add(classA);
        listOfClasses.Add(classB);
        listOfClasses.Add(classC);


        InitializeComponent();
        aListBox.ItemsSource = listOfClasses;
    }

    private void buttonOne_Click(object sender, RoutedEventArgs e)
    {
        foreach (myClass cl in listOfClasses)
            if (cl.SwitchVisible)
                cl.SwitchVisible = false;
            else
                cl.SwitchVisible = true;
    }
}

public class myClass
{
    private string _aValue;
    private bool _switchVisible;

    public bool SwitchVisible { get { return _switchVisible; } set { _switchVisible = value; visControl.changeVisibility(_switchVisible); } }
    public string aValue { get { return _aValue; } }
    public controlProperties visControl;


    public myClass(string invalue)
    {
        visControl = new controlProperties();
        visControl.VisibleState = Visibility.Visible;
        _aValue = invalue;
    }

}

public class controlProperties
{
    private Visibility _visibility;


    public Visibility VisibleState { get { return _visibility; } set { _visibility = value; } }

    public void changeVisibility(bool isVisible)
    {
        if (isVisible)
            _visibility = Visibility.Visible;
        else
            _visibility = Visibility.Collapsed;
    }
}

任何想法,如果这是一个路径问题或绑定问题?

1 个答案:

答案 0 :(得分:1)

如果您希望在更改属性值时自动更新控件,则您的类必须实现INotifyPropertyChanged接口。

例如:

public class controlProperties : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Visibility _visibility;

    public Visibility VisibleState
    {
        get
        {
            return _visibility;
        }

        set
        {
            _visibility = value;
            this.NotifyPropertyChanged("VisibleState");
        }
    }

    public void changeVisibility(bool isVisible)
    {
        if (isVisible)
            this.VisibleState = Visibility.Visible;
        else
            this.VisibleState = Visibility.Collapsed;
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        var eventHandler = this.PropertyChanged;

        if (eventHandler != null)
        {
            eventHandler(sender, new PropertyChangedEventArgs(propertyName));
        }
    }
}