开发者

How to put few custom controls dynamicaly inside a datatemplete on Mouse behavior

开发者 https://www.devze.com 2023-02-20 15:00 出处:网络
I have a Datatemplete for List-box Item in which I have a Grid with two columns using WPF. In the first column I want to put few customized controls(Buttons) dynamically using C# in code behind. I don

I have a Datatemplete for List-box Item in which I have a Grid with two columns using WPF. In the first column I want to put few customized controls(Buttons) dynamically using C# in code behind. I don't know how to start and from where should I start, can anybody please help me with some great inputs and examples. Any answer will be greatly appreciate.

Thanks in advance.

XAML code:

<ListBox x:Name="ListBoxItem"
                     Grid.Row="1"
                     SelectionMode="Extended"
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                     VirtualizingStackPanel.IsVirtualizing="True" 
                     VirtualizingStackPanel.VirtualizationMode="Recycling"
                     FocusVisualStyle="{x:Null}"
                     KeyboardNavigation.IsTabStop="False"
                     Background="DarkGray"
                     ItemsSource="{Binding}">

                <ListBox.ItemsPanel>

                    <ItemsPanelTemplate>

                        <WrapPanel x:Name="ListContent"
                                   IsItemsHost="True"
                                   Width="500"/>

                    </ItemsPanelTemplate>

            </ListBox.ItemsPanel>

            <ListBox.ItemTemplate>

                <DataTemplate>

                        <DockPanel LastChildFill="True"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center">

                            <StackPanel DockPanel.Dock="Left"
                                        Width="30"
                                        Height="{Binding Height}">
                               <--Here I want to put few customize buttons in code behind-->
                            </StackPanel>

                            <Image x:Name="MainPage" 
                                   Stretch="UniformToFill"
                                   Source="{Binding ImagePath}"
                                   Height="{Binding He开发者_如何学JAVAight}"
                                   Width="{Binding Width}"/>

                        </DockPanel>

                </DataTemplate>

            </ListBox.ItemTemplate>


You specified wanting to use code behind, so it would look something like this:

XAML:

<StackPanel Initialized="StackPanel_Initialized" .. />

Code behind:

using MyNamespace;

private void StackPanel_Initialized(object sender, EventArgs e)
{
    MyControl newItem = new MyControl();
    // Set any other properties

    StackPanel parent = sender as StackPanel;
    parent.Children.Add(newItem);
}


If you are looking for adding Controls inside a the First column of your grid then put a Panel inside the first column and in code behind add controls as child to that Panel. So as you mentioned in above that you are using DataTemplete then I would like to say that you can access that Panel something like:

Put the below codes inside the event where you wnt to add the controls.

ListBoxItem item = (ListBoxItem)(this.lst.ItemContainerGenerator.ContainerFromIndex(i));
ContentPresenter presenter = FindVisualChild<ContentPresenter>(item);
DataTemplate template = presenter.ContentTemplate;
StackPanel stack = (StackPanel)template.FindName("FirstColumn Panel Name", presenter);

and then call the below method:

private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
       for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
       {
               DependencyObject child = VisualTreeHelper.GetChild(obj, i);
               if (child != null && child is childItem)
                     return (childItem)child;
               else
               {
                     childItem childOfChild = FindVisualChild<childItem>(child);
                     if (childOfChild != null)
                          return childOfChild;
               }
        }
        return null;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号