使用Event在运行时进行Datatemplate

时间:2013-10-31 12:03:15

标签: c# windows-phone-7 events windows-phone-8 datatemplate

我知道,如何在运行时使用c#Code:

创建Datatemplates
string xaml =
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
...
...
</DataTemplate>";

DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);

但是我如何向此模板添加事件以及我应该在哪里定义Eventhandler。

2 个答案:

答案 0 :(得分:0)

最简单的方法:

例如,你有一个ListBox:

testLisBox.ItemTemplate = CreateTemplate();
testLisBox.ItemsSource = new[] { "Item1", "Item2" };    
testLisBox.AddHandler(Button.ClickEvent, new RoutedEventHandler(buttonClick));

private DataTemplate CreateTemplate()
        {          
            string xaml =
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<Button Name=""testbutton"">123123</Button>
</DataTemplate>";
            return (DataTemplate)System.Windows.Markup.XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)));            
        }

private void buttonClick(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button && ((Button)e.OriginalSource).Name == "testbutton")
            {
                MessageBox.Show("123");    
            }            
        }

答案 1 :(得分:0)

我找到了解决方案:

LongListMultiSelector LLMS = new LongListMultiSelector();
LLMS.ItemTemplate = CreateDataTemplate();
LLMS.ItemsSource = ExampleList;

浏览LongListMultiSelector中的所有项目:

int number = 0;
for(int i; i<ExampleList.Count; i++)
{
    number = 0;
    StackPanel sp = FindElementInVisualTree<StackPanel>(LLMS, i);
    sp.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(StackPanel_Tap);
}

此方法在父元素中找到一个元素,其索引为:

private T FindElementInVisualTree<T>(DependencyObject parentElement, int ind) where T : DependencyObject
{
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0) 
            return null;

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(parentElement, i);
            if (child != null && child is T)
            {
                if (number == ind)
                {
                    number = 0;
                    return (T)child;
                }
                number++;
            }
            else
            {
                var result = FindElementInVisualTree<T>(child, ind);
                if (result != null)
                    return result;
            }
        }
        return null;
}

private DataTemplate CreateDataTemplate()
{
    string xaml =
        @"<DataTemplate
        xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
        xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">

            <StackPanel>
                <TextBlock Text='{Binding Title}'/>
                ...
                ...
            </StackPanel>
        </DataTemplate>";

    DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);

    return dt;
}