[英]Converting a local control in XAML to C#
我正在做一个WPF应用程序,该应用程序以枢轴显示图像,并添加了一个小的控件以小图标显示所有图像在屏幕底部。当我用一些简单的图像对其进行测试时,一旦添加,读取服务器并处理控件无效文件的代码,因为一旦我动态添加数据透视表项,就不再调用它。
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#代码
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"/>
我相信您需要将pivot
变量设置为视图模型中的一个属性,然后在该属性中调用OnPropertyChanged
。 像这样:
XAML代码:(请注意,它绑定到公共属性Pivot而不是var枢轴)
<local:PivoteLocationView="{Binding ElementName=Pivot}"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Margin="0,0,0,10"/>
将添加数据透视表项的C#代码(请注意,数据透视表=数据透视表的末尾)
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.