1

I'm doing a WPF application that shows images in a Pivot, and added a small control to show at the bottom of the screen all the images in small icons, When I test it with some simple images it all Works, once I add the code that reads the server, and processes the files the control doesn’t Works, because it’s no longer called once I dynamically add the pivot items.

XAML Code:

<local:PivoteLocationView="{Binding ElementName=pivot}"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Bottom"
                          Margin="0,0,0,10"/>

<controls:Pivot Margin="0,-30,0,40" 
                x:Name="pivot">
  <controls:PivotItem>
    ...
  </controls:PivotItem>
</controls:Pivot>

C# code that will add the pivotitems

    pivot.Items.Clear();
    for (i = 0; i < total; i++)
    {
        var rect = new Rectangle();
        var btxt = new TextBlock();
        var stackend = new StackPanel();
        var PIend = new PivotItem();

        ... code that assigns values

        stacktend.Children.Add(rect);
        stacktend.Children.Add(btxt);

        PIend.Content = stacktend;

        pivot.Items.Add(pantodo);

    }

The control:

public class PivotLocationViewModel
{
  private Pivot _pivot;

  public PivotLocationViewModel()
  {
  }

  public PivotLocationViewModel(Pivot pivot)
  {
    PivotItems = new PivotItemViewModelCollection();
    SetPivot(pivot);
  }

public PivotItemViewModelCollection PivotItems { get; set; }

  private void SetPivot(Pivot pivot)
  {
    _pivot = pivot;

    // handle selection changed
    pivot.SelectionChanged += Pivot_SelectionChanged;

    // create a view model for each pivot item.
    for(int i=0;i<pivot.Items.Count;i++)
    {
      PivotItems.Add(new PivotItemViewModel());
    }

    PivotItems[_pivot.SelectedIndex].IsSelected = true;
  }

 private void  Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
      var selectedModel = PivotItems.SingleOrDefault(p => p.IsSelected);
    if (selectedModel != null)
      selectedModel.IsSelected = false;

    PivotItems[_pivot.SelectedIndex].IsSelected = true;
  }
}
public class PivotItemViewModelCollection : List<PivotItemViewModel>
{
}

public class PivotItemViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    #endregion

    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (_isSelected == value)
                return;

            _isSelected = value;
            OnPropertyChanged("IsSelected");

            Color = IsSelected ? Colors.Black : Colors.White;
        }
    }

    private Color _color = Colors.White;

    public Color Color
    {
        get { return _color; }
        set
        {
            _color = value;
            OnPropertyChanged("Color");
        }
    }

}

As a solución I’m thinking about adding this code dynamically instead of using it in the xaml file but have no idea how, any help will be appreciated.

<local:PivotLocationView Source="{Binding ElementName=pivot}"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Bottom"
                             Margin="0,0,0,10"/>
4

1 回答 1

0

我相信您需要pivot在视图模型中将变量设为属性,然后调用OnPropertyChanged该属性。像这样的东西:

XAML 代码:(注意它绑定到公共属性 Pivot 而不是 var pivot)

<local:PivoteLocationView="{Binding ElementName=Pivot}"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Bottom"
                      Margin="0,0,0,10"/>

将添加数据透视项的 C# 代码(注意末尾的 Pivot = pivot)

pivot.Items.Clear();
for (i = 0; i < total; i++)
{
     //....nothing really changes here
}
Pivot = pivot;

添加到控件:

public Pivot Pivot
{
    get
    {
        return pivot;
    }
    set
    {
        pivot = value;
        OnPropertyChanged("Pivot");
    }
}
于 2013-09-28T20:54:58.543 回答