我在列表框中有一个组合框,listbox有一个datatemplate,其中有这个组合框和其他元素。
<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>
我怀疑是我无法访问列表框的数据窗口内存在的组合框, 我想从c#codebehind
将列表绑定到组合框cmbbxbUMselec.DataContext = lstumcods;
以这种方式从代码后面但我无法访问列表框的datatemplate中存在的组合框
请让我知道如何解决这个问题。
提前致谢
答案 0 :(得分:0)
如果您不介意在XAML中进行操作而不是代码隐藏,那么这是一个想法。
我假设您的ListBox在UserControl中的某个位置定义,如下所示:
<UserControl x:Name="MyUserControl">
<!-- Other elements here -->
<ListBox x:Name="lstbxbProducts">
<ListBox.ItemTemplate>
<!-- etc... -->
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
如果lstumcods是在UserControl的代码隐藏中定义的公共属性,那么你可以试试这个:
<ComboBox ItemsSource="{Binding lstumcods, ElementName=MyUserControl" />
答案 1 :(得分:0)
试试这个:
首先我已将您的列表框更改为:
<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>
请注意,我在listbox.resources中添加了一个项目。
在代码隐藏中,我添加了以下代码:
var cb = lstbxbProducts.Resources.FirstOrDefault(c => c.Key.ToString() == "myComboBox");
((ComboBox)cb.Value).ItemsSource = new List<string> { "A", "B", "C" };
不要忘记将“System.Linq”添加到程序集中,否则它将无法正常工作。 现在尝试使用您的“真实”来源并告诉我们它是否有效。
希望它有所帮助。