将XAML中的本地控件转换为C#

时间:2013-09-28 17:59:26

标签: c# wpf xaml binding controls

我正在做一个在Pivot中显示图像的WPF应用程序,并添加了一个小控件,以小图标显示屏幕底部的所有图像,当我用一些简单的图像测试它时,它都是Works,一次我添加了读取服务器的代码,并处理控件无法正常工作的文件,因为一旦我动态添加了数据透视项,它就不再被调用了。

XAML代码:

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

将添加pivotitems的C#代码

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

    }

控件:

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

}

作为一个解决方案我正在考虑动态添加此代码,而不是在xaml文件中使用它,但不知道如何,任何帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:0)

我认为您需要在viewmodel中将pivot变量设为属性,然后在属性中调用OnPropertyChanged。像这样:

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

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

将添加pivotitems的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");
    }
}