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