简体   繁体   English

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

[英]Converting a local control in XAML to C#

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. 我正在做一个WPF应用程序,该应用程序以枢轴显示图像,并添加了一个小的控件以小图标显示所有图像在屏幕底部。当我用一些简单的图像对其进行测试时,一旦添加,读取服务器并处理控件无效文件的代码,因为一旦我动态添加数据透视表项,就不再调用它。

XAML Code: 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>

C# code that will add the 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);

    }

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. 作为一个解决方案,我正在考虑动态添加此代码,而不是在xaml文件中使用它,但是不知道如何,将不胜感激。

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

I believe that you need to make your pivot variable a property in your viewmodel then call OnPropertyChanged in the property. 我相信您需要将pivot变量设置为视图模型中的一个属性,然后在该属性中调用OnPropertyChanged Something like this: 像这样:

XAML code: (notice its bound to the public property Pivot instead of the var pivot) XAML代码:(请注意,它绑定到公共属性Pivot而不是var枢轴)

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

C# code that will add the pivotitems (notice the Pivot = pivot at the end) 将添加数据透视表项的C#代码(请注意,数据透视表=数据透视表的末尾)

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

Add to the control: 添加到控件:

public Pivot Pivot
{
    get
    {
        return pivot;
    }
    set
    {
        pivot = value;
        OnPropertyChanged("Pivot");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM