我有一个ListView
,它显示具有不同列的表。表中一行的每个单元格都包含不同类型的控件。我试图允许用户通过选择一行并双击以使单元格可编辑来编辑每一行中的数据。因此,除了包含ComboBox
es的列之外,我都能使所有其他功能正常工作。
XAML代码:
这是ListView
的XAML代码。它大约有7列,但我专注于此处显示的ComboBox
es列。
<ListView x:Name="MyListView" IsSynchronizedWithCurrentItem="True" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,12,0,0" Height="315" Width="560" ItemsSource="{Binding People}">
<ListView.View>
<GridView>
<!-- More Grid column code here -->
<GridViewColumn Header="Fleet" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="locationCmboBx" ItemsSource="{Binding DataContext.SchoolLocations, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}" Loaded="OnCmboBxLoad" IsEnabled="False" Width="55" HorizontalAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<!-- More Grid column code here -->
</GridView>
</ListView.View>
</ListView>
C#代码:
因此在下面的代码中,我尝试使用其他人推荐的VisualTreeHelper
来访问嵌套在{内的locationsCmboBx
( ComboBox ) DataTemplate
中的{1}},CellTemplate
和其他 XAML 标头。
ListView
因此,我所拥有的所有东西都可以正常工作,但是当我调试代码并进入// More code before here
ListView listViewItem = (ListView)(MyListView.ItemContainerGenerator.ContainerFromItem(MyListView));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(listViewItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
ComboBox comboBox = (ComboBox)myDataTemplate.FindName("locationsCmboBx", myContentPresenter);
// More code before here
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;
}
函数FindName
时,ComboBox
就可以了。最终,我想在其上设置null
属性,并从IsEnabled
获取SelectedValue
。我相信我缺少了一些东西,但不确定什么。任何帮助将不胜感激?
答案 0 :(得分:1)
您的代码中的问题是XAML pip3 install pandas
Collecting pandas
Starting new HTTPS connection (1): pypi.org
Exception:
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 143, in main
status = self.run(options, args)
File "/usr/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 318, in run
resolver.resolve(requirement_set)
File "/usr/lib/python3.7/site-packages/pip/_internal/resolve.py", line 102, in resolve
self._resolve_one(requirement_set, req)
File "/usr/lib/python3.7/site-packages/pip/_internal/resolve.py", line 256, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "/usr/lib/python3.7/site-packages/pip/_internal/resolve.py", line 209, in _get_abstract_dist_for
self.require_hashes
File "/usr/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 218, in prepare_linked_requirement
req.populate_link(finder, upgrade_allowed, require_hashes)
File "/usr/lib/python3.7/site-packages/pip/_internal/req/req_install.py", line 164, in populate_link
self.link = finder.find_requirement(self, upgrade)
File "/usr/lib/python3.7/site-packages/pip/_internal/index.py", line 572, in find_requirement
all_candidates = self.find_all_candidates(req.name)
File "/usr/lib/python3.7/site-packages/pip/_internal/index.py", line 530, in find_all_candidates
for page in self._get_pages(url_locations, project_name):
File "/usr/lib/python3.7/site-packages/pip/_internal/index.py", line 675, in _get_pages
page = self._get_page(location)
File "/usr/lib/python3.7/site-packages/pip/_internal/index.py", line 793, in _get_page
return _get_html_page(link, session=self.session)
File "/usr/lib/python3.7/site-packages/pip/_internal/index.py", line 144, in _get_html_page
"Cache-Control": "max-age=0",
File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 473, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python3.7/site-packages/pip/_internal/download.py", line 396, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3.7/site-packages/cachecontrol/adapter.py", line 53, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/usr/lib/python3.7/site-packages/requests/adapters.py", line 437, in send
return self.build_response(request, resp)
File "/usr/lib/python3.7/site-packages/cachecontrol/adapter.py", line 107, in build_response
if response.chunked:
AttributeError: 'HTTPResponse' object has no attribute 'chunked'
中组合框名称的拼写错误,而后面的代码是locationCmboBx
s location
。
代码:
CmboBx
也是错误的。 ListView listViewItem = (ListView)(MyListView.ItemContainerGenerator.ContainerFromItem(MyListView));
的参数必须是一个数据项。返回类型也是错误的。它必须是ContainerFromItem()
我建议您使用ViewModel +绑定,而不要使用后面的代码来访问数据。因此,您可以避免出现此类Tipo错误。另请参阅:Detect in XAML broken bindings already at compile time