【问题标题】:What's the best way of changing a ListBox's UniformGrid column count?更改 ListBox 的 UniformGrid 列数的最佳方法是什么?
【发布时间】:2013-06-26 20:28:59
【问题描述】:

我有一个带有UniformGrid 布局的ListBox,我正在尝试更改“列”属性,但我不知道最好的方法。我尝试绑定到属性或以编程方式创建新布局,但我无法弄清楚。

<ListBox x:Name="ImagesList" ItemsSource="{Binding Path=GridImages}">
        <ListBox.ItemsPanel>

            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True" Columns="3" VerticalAlignment="Top" />
            </ItemsPanelTemplate>

        </ListBox.ItemsPanel>
</ListBox>

当用户单击两个按钮时,我正在尝试在 1 列和 3 列之间进行更改。我尝试与Columns="{Binding Path=MyColumnCount}" 绑定,但它从未改变,并尝试设置x:Name 并从我的代码访问,但没有成功。我还尝试实例化一个新的UniformGrid,但我已经读到我需要一个工厂,所以我不能设置不同的Columns 值。

【问题讨论】:

  • 你尝试过什么?请提供更多代码。
  • 何时需要更改列数?您的代码中应该触发什么?
  • @newStackExchangeInstance 我已经用更多信息更新了这个问题。你需要实际的代码吗?我上班的时候可以拿到。
  • @Sphinxxx 我在用户点击某些按钮时触发。我已经用更多细节更新了这个问题。谢谢!
  • MyColumnCount 属性的代码是什么?

标签: c# wpf listbox wpf-controls


【解决方案1】:

我想也许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>

试试这个简单的 ViewModel,它会在计时器上更新 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) => { };
}

【讨论】:

  • 谢谢,现在绑定工作了!唯一的问题是我必须设置DataContext 我希望刷新属性的所有内容。喜欢ImagesList.DataContext = null; ImagesList.DataContext = this;。但这可能是因为我想我直接绑定到MyWin : Windows。这不是Something : INotifyPropertyChanged
  • 好的,我明白了。 INotifyPropertyChanged 及其 PropertyChanged 事件是更新绑定值的秘密。但是如果MyColumnCount 是你Window 中的一个属性,你可以把它变成一个DependencyProperty,它有自己神奇的更新机制。这篇文章有一些有用的链接:What is a dependency property?
  • 哇,对我帮助很大!!谢谢!! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 2015-10-09
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
相关资源
最近更新 更多