0

我有一个问题,我不确定它是什么。我在一个类中有一个类,它的值需要绑定到控件,在这种情况下是可见性。代码正确更改了值,但输出没有更改(即折叠控件)

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

如果这是一个路径问题或绑定问题,有什么想法吗?

4

1 回答 1

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));
        }
    }
}
于 2012-11-02T15:00:52.223 回答