WPF: WrapPanel 容器的模板數據綁定(ItemsControl)


問題:

       有一些CheckBox需要作為選項添加到頁面上,但是數目不定。而為了方便排版,我選擇用WrapPanel面板來作為父容器。那現在的問題就是如何把這些控件添加到這個WrapPanel里了。我想到了兩個方法,第一個是先得到控件數目,然后再動態生成並加載到這個WrapPanel里,第二個是設置數據綁定。我想第一個是可行的,但是項目中還涉及到其它問題,所以這里就選擇第二個了。問題來了,在WrapPanel中並沒有可以用來設置綁定並實現動態生成的東西,那要怎么解決了?

辦法:

新建一個ItemsControl控件,並為ItemsSource綁定數據源,然后把ItemsControl.ItemsPanel設置為WrapPanel,最后為ItemsControl.ItemTemplate中的CheckBox.Content綁定數據。

 

eg:

1、創建數據源類型。

public class business
{
public string txt { get; set; }
}
 
2、設置數據源

public MainWindow()
{
this.InitializeComponent();
List<business> che = new List<business>()
{
new business() { txt = "選項1"},
new business() { txt = "選項2"},
new business() { txt = "選項3"},
new business() { txt = "選項4"},
new business() { txt = "選項5"},
new business() { txt = "選項6"},
new business() { txt = "選項7"}
};
ItemsControl.ItemsSource = che;
}
3、Xaml中

<ItemsControl x:Name="itemsControl" Background="#B28BB2F1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Padding="3">
<WrapPanel>
<CheckBox Content="{Binding txt}"/>
</WrapPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
調試一下就OK了。

 


下一篇告訴你怎么遍歷這個DataTemplate,並判斷哪些checkBox被選中了。
————————————————————————————————————
原文鏈接:https://blog.csdn.net/wushang923/article/details/6739756

================================================================================================

情況1:在設定DataTemplate的Name,並且他是在前台表示時,獲取DataTemplate里的指定控件。

方法:

http://blog.csdn.net/wackelbh/article/details/6003947(參考這篇文章)

 


情況2:當沒有設定DataTemplate的Name或是以Resource方式調用時,獲取DataTemplate里的指定控件。
方法:

1、這里需要有一個從DataTemplate里獲取控件的函數
public T FindFirstVisualChild<T>(DependencyObject obj, string childName) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T && child.GetValue(NameProperty).ToString() == childName)
{
return (T)child;
}
else
{
T childOfChild = FindFirstVisualChild<T>(child, childName);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return null;
}

2、稍微改動一下前篇里的代碼:
<ItemsControl x:Name="itemsControl" Background="#B28BB2F1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Padding="3">
<WrapPanel>
<TextBox x:Name="txtID"/>
<TextBlock x:Name="txtName" Text="Good"/>
</WrapPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
或者

<Page.Resource>
<DataTemplate x:Key="data">
<Border Padding="3">
<WrapPanel>
<TextBox x:Name="txtID"/>
<TextBlock x:Name="txtName" Text="Good"/>
</WrapPanel>
</Border>
</DataTemplate>
</Page.Resources>

<ItemsControl x:Name="itemsControl" Background="#B28BB2F1" ItemTemplate="{StaticResource data}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>

3、解下來就寫按鈕的處理函數:
我需要獲取DataTemplate里名為"txtName"的TextBlock控件並顯示他的Text內容。

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
TextBlock txt = FindFirstVisualChild<TextBox>(itemsControl, "txtName");
if (txt != null)//判斷是否找到
MessageBox.Show(txt.Text.ToString());
}

 

情況3:當沒有設定DataTemplate的里的控件Name或者你壓根不知道里面有哪些控件,但是你又想獲取他們的值時。例如上一篇,當我動態生成CheckBox后,我想知道哪些CheckBox被選中了。


方法:

1、也需要一個獲取DataTemplate控件的函數,但是返回的是一個集合。
public List<T> GetChildObjects<T>(DependencyObject obj, string name) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).Name == name || string.IsNullOrEmpty(name)))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child, ""));//指定集合的元素添加到List隊尾
}
return childList;
}

2、xaml中代碼(詳細請看前一篇)
<ItemsControl x:Name="itemsControl" Background="#B28BB2F1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Padding="3">
<WrapPanel>
<CheckBox Content="{Binding txt}"/>
</WrapPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

3、解下來就寫按鈕的處理函數:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
DataVisualTreeHelper VTHelper = new DataVisualTreeHelper();
List<CheckBox> collection = VTHelper.GetChildObjects<CheckBox>(itemsControl, "")//第2個參數為空,表示查找所有指定類型的控件(返回

一個CheckBox集合)
foreach (CheckBox item in collection //遍歷這個集合
{
if (item.IsChecked == true)
MessageBox.Show(item.Content.ToString() + "被選中了!");
}
}


先寫到這了,以后有發現更好的方法再補上。
———————————————————————————
原文鏈接:https://blog.csdn.net/wushang923/article/details/6742378


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM