首页 / C# / C#-向添加到列表框中的控件触发事件
C#-向添加到列表框中的控件触发事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-向添加到列表框中的控件触发事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2771字,纯文字阅读大概需要4分钟。
内容图文
![C#-向添加到列表框中的控件触发事件](/upload/InfoBanner/zyjiaocheng/661/0cd289569ab849ffab5e02508780aca6.jpg)
我想知道将ListBoxItems添加到ListBox时触发哪个事件.请注意,数据更改时我不希望发生任何事件.我想要添加控件时的事件.
我已经在这个答案中提到他们说使用CollectionChaged事件,当Collection更改时会触发.所以我不能使用它,因为在将控件添加到VisualTree之前会触发该事件.
您可能在想为什么我需要它.我只是想将列表框的宽度更改为最宽项目的宽度.如果您对我要实现的目标更感兴趣,请查看我的代码:
private void SomeEvent(object sender, ............... e)
{
double greatestWidth = 0;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(sidebar) - 1; i++)
{
ListBoxItem li = VisualTreeHelper.GetChild(sidebar, i) as ListBoxItem;
li.HorizontalAlignment = HorizontalAlignment.Center;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(sidebar) - 1; i++)
{
ListBoxItem li = VisualTreeHelper.GetChild(sidebar, i) as ListBoxItem;
if (li.Width > greatestWidth)
{
greatestWidth = li.Width;
}
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(sidebar) - 1; i++)
{
ListBoxItem li = VisualTreeHelper.GetChild(sidebar, i) as ListBoxItem;
li.HorizontalAlignment = HorizontalAlignment.Stretch;
}
}
更新:
class ResizablePanel : StackPanel
{
protected override Size MeasureOverride(Size constraint)
{
Size calculatedSize = base.MeasureOverride(constraint);
foreach (ListBoxItem li in this.Children)
{
li.HorizontalAlignment = HorizontalAlignment.Center;
}
double greatestWidth = 0;
foreach (ListBoxItem li in this.Children)
{
if (li.Width > greatestWidth)
{
greatestWidth = li.Width;
}
}
foreach (ListBoxItem li in this.Children)
{
li.HorizontalAlignment = HorizontalAlignment.Stretch;
}
calculatedSize = new Size(greatestWidth, calculatedSize.Height);
return calculatedSize;
}
}
解决方法:
如此处所讨论的,您将如何控制大多数大小调整方面
首先定义一个新面板并覆盖MeasureOverride方法
namespace CSharpWPF
{
class MyPanel : StackPanel
{
protected override Size MeasureOverride(Size constraint)
{
Size calculatedSize = base.MeasureOverride(constraint);
foreach (ListBoxItem item in this.Children)
{
//your logic with each item
}
return calculatedSize;
}
}
}
每当添加或删除任何项目或需要更改布局(即调整容器大小)时,都会调用MeasureOverride方法.
foreach(this.Children中的ListBoxItem项)假定您仅与ListBox一起使用.您可以根据需要进行更改.
然后将此新面板用作
<ListBox xmlns:l="clr-namespace:CSharpWPF">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<l:MyPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
通过这种方法,您可以有效地控制尺寸
阅读更多关于FrameworkElement.MeasureOverride
在查看示例之后,这里是您需要做的更改以获得所需的
>将Margin =“ 10,5,10,5”设置为边栏ListBox
>将Horizo??ntalAlignment =“ Right”添加到侧边栏ListBox
>从metro.xaml第35行的ContentPresenter中删除Horizo??ntalAlignment =“ Center”
>设置< Setter Property =“ Horizo??ntalAlignment” Value =“ Stretch” /> metro.xaml第51行
>添加< Setter Property =“ TextBlock.TextAlignment” Value =“ Right” />在第52行
内容总结
以上是互联网集市为您收集整理的C#-向添加到列表框中的控件触发事件全部内容,希望文章能够帮你解决C#-向添加到列表框中的控件触发事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。