我想也許ItemsPanelTemplate
沒有繼承ListBox
'DataContext
,但它,所以你應該Binding
工作:
<ListBox x:Name="ImagesList" ItemsSource="{Binding Path=GridImages}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True" Columns="{Binding Path=MyColumnCount}"
VerticalAlignment="Top" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
這個簡單的視圖模型,其上的定時器更新MyColumnCount
試試:
public class ImagesVM : INotifyPropertyChanged
{
private System.Threading.Timer _timer;
private int _colIncrementor = 0;
public ImagesVM()
{
_timer = new System.Threading.Timer(OnTimerTick, null,
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1));
_gridImages = new string[] {
"http://www.anbg.gov.au/images/flags/semaphore/a-icon.gif",
"http://www.anbg.gov.au/images/flags/semaphore/b-icon.gif",
"http://www.anbg.gov.au/images/flags/semaphore/c-icon.gif",
"http://www.anbg.gov.au/images/flags/semaphore/d-icon.gif",
"http://www.anbg.gov.au/images/flags/semaphore/e-icon.gif",
"http://www.anbg.gov.au/images/flags/semaphore/f-icon.gif",
};
}
private void OnTimerTick(object state)
{
this.MyColumnCount = (_colIncrementor++ % 3) + 1;
}
private int _myColumnCount = 3;
public int MyColumnCount
{
get { return _myColumnCount; }
set
{
_myColumnCount = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("MyColumnCount"));
}
}
private string[] _gridImages = null;
public string[] GridImages
{
get { return _gridImages; }
}
public event PropertyChangedEventHandler PropertyChanged = (s, e) => { };
}
你試過的是什麼?請多些代碼。 –
您何時需要更改列數?你的代碼應該觸發什麼? – Sphinxxx
@newStackExchangeInstance我用更多的信息更新了這個問題。你需要實際的代碼嗎?當我開始工作時我可以得到它。 –