[英]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.