Accessing ComboBox inside a Datatemplate of a listbox from c# code behind in metro application?

 ̄綄美尐妖づ 提交于 2019-12-25 06:09:41

问题


i have a combo box present inside a list-box , listbox has a datatemplate in which this combobox and other elements resides .

<ListBox x:Name="lstbxbProducts" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel Width="80" Orientation="Horizontal">
                           <TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>     
                           <TextBlock Text="  -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
                        </StackPanel>
                        <StackPanel Width="400">
                            <TextBlock Text="{Binding prddsc}" VerticalAlignment="Center"  HorizontalAlignment="Left" ></TextBlock>
                        </StackPanel>   
                       <StackPanel Width="300">
                            <ComboBox Name="cmbbxbUMselec" ItemsSource="{Binding}" Width="200" FontSize="24"  VerticalAlignment="Center" HorizontalAlignment="Center" ></ComboBox>
                        </StackPanel>
                        <StackPanel Width="180">
                            <TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
                        </StackPanel>
                       <StackPanel Width="100">
                            <TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center"                      HorizontalAlignment="Right" ></TextBox>
                        </StackPanel>

                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My doubt is that i am not able access the combobox present inside a the datatemplate of the listbox , i want to bind a list to the combobox from c# codebehind

cmbbxbUMselec.DataContext = lstumcods;

in this way from code behind but i am not able access the combobox present inside a the datatemplate of the listbox

Please let me know how can i work around this.

Thanks in advance


回答1:


Try this:

First i've changed your listbox to this :

<ListBox x:Name="lstbxbProducts" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
        <ListBox.Resources>
            <ComboBox x:Key="myComboBox" Name="cmbbxbUMselec" ></ComboBox>
        </ListBox.Resources>           
       <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <StackPanel Width="80" Orientation="Horizontal">
                       <TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>     
                       <TextBlock Text="  -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
                    </StackPanel>
                    <StackPanel Width="400">
                        <TextBlock Text="{Binding prddsc}" VerticalAlignment="Center"  HorizontalAlignment="Left" ></TextBlock>
                    </StackPanel>   
                   <StackPanel Width="300">
                        <ComboBox Name="cmbbxbUMselec" ItemsSource="{Binding Source={StaticResource myComboBox}, Path=ItemsSource, Mode=TwoWay}"
                                    Width="200" FontSize="24"  VerticalAlignment="Center" HorizontalAlignment="Center" ></ComboBox>
                    </StackPanel>
                    <StackPanel Width="180">
                        <TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
                    </StackPanel>
                   <StackPanel Width="100">
                        <TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center"                      HorizontalAlignment="Right" ></TextBox>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Note that i've added a item in your listbox.resources.

On code-behind i've added the following code:

            var cb = lstbxbProducts.Resources.FirstOrDefault(c => c.Key.ToString() == "myComboBox");
        ((ComboBox)cb.Value).ItemsSource = new List<string> { "A", "B", "C" };

Don't forget to add "System.Linq" to your assemblies, otherwise it will not work. Now try it with your "true" source and tell us if its working.

Hope it helps.




回答2:


If you don't mind doing it in XAML rather than code-behind, here's an idea.

I assume your ListBox is defined somewhere inside a UserControl, like this:

<UserControl x:Name="MyUserControl">
    <!-- Other elements here -->
    <ListBox x:Name="lstbxbProducts">
        <ListBox.ItemTemplate>
            <!-- etc... -->
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl>

If lstumcods is a public property defined in the code-behind of the UserControl, then you could try this:

<ComboBox ItemsSource="{Binding lstumcods, ElementName=MyUserControl" />


来源:https://stackoverflow.com/questions/12370081/accessing-combobox-inside-a-datatemplate-of-a-listbox-from-c-sharp-code-behind-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!