我想访问MainWindow.xaml.cs中的ComboBox项(在另一个类中定义),但是我不能。
我是C#和WPF的新手。此代码的目的是获取选定的ComboBox项作为搜索键。我已经从Internet上的许多示例代码中复制了代码,现在我完全迷路了。我什至不知道哪一部分错了。所以,让我显示整个代码(对不起):
MainWindow.xaml:
<?php
function print_d($array){ echo "<pre>\n"; print_r($array); echo "</pre>\n";}
$mytoken = "*************mytoken ****************";
$devId = "**************devId ***************";
$appId = "*************appId ***************";
$certId = "**************certId **************";
$wsdl_url = 'http://developer.ebay.com/webservices/latest/ebaySvc.wsdl';
$apiCall = "GetSellerList";
$credentials = array('AppId' => $appId, 'DevID' => $devId, 'AuthCert' => $certId);
$client = new SOAPClient($wsdl_url, array('trace' => 1, 'exceptions' => 0, 'location' => "https://api.ebay.com/wsapi?callname=$apiCall&appid=$appId&siteid=0&version=871&Routing=new"));
$eBayAuth = array('eBayAuthToken' => new SoapVar($mytoken, XSD_STRING, NULL, NULL, NULL, 'urn:ebay:apis:eBLBaseComponents'),
'Credentials' => new SoapVar ($credentials, SOAP_ENC_OBJECT, NULL, NULL, NULL, 'urn:ebay:apis:eBLBaseComponents'));
$header_body = new SoapVar($eBayAuth, SOAP_ENC_OBJECT);
$header = array(new SOAPHeader('urn:ebay:apis:eBLBaseComponents', 'RequesterCredentials', $header_body));
//set the API call parameters
$params = array('UserID'=>'**userid**','DetailLevel'=>'ReturnAll','GranularityLevel'=>'Coarse','WarningLevel'=>'High','IncludeWatchCount'=>'true','Pagination'=>array('EntriesPerPage'=>20,'PageNumber'=>1),'Version' => 871, 'StartTimeFrom'=>'2019-07-01T01:01:02.768Z' ,'StartTimeTo'=>'2019-08-22T01:01:02.768Z');
$request = $client->__soapCall($apiCall, array($params), NULL, $header); //make the actual API call
print_d($request);
?>
MainWindow.xaml.cs:
<Window x:Class="XY.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XY"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="454.4">
<Grid>
<DataGrid ItemsSource="{Binding channels}"
SelectedItem="{Binding SelectedRow, Mode=TwoWay}"
Margin="0,0,0,-0.2">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding name}"
Header="Channel" Width="Auto"/>
<DataGridTemplateColumn Width="100" Header="Point Setting">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="piontsComboBox"
ItemsSource="{Binding DataContext.points,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
SelectionChanged="PrintText"
DisplayMemberPath="name"
SelectedValuePath="name"
Margin="5"
SelectedItem="{Binding DataContext.SelectedPoint,
RelativeSource={RelativeSource AncestorType={x:Type Window}},
Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<TextBox x:Name="tb" Width="140" Height="30" Margin="10,250,200,30"></TextBox>
<Button x:Name="Browse_Button" Content="Browse" Margin="169,255,129.6,0"
Width="75" Click="Browse_Button_Click" Height="30" VerticalAlignment="Top"/>
</Grid>
Point.cs:
using System;
using System.Windows;
using System.Windows.Controls;
namespace XY
{
public partial class MainWindow : Window
{
public GridModel gridModel { get; set; }
public MainWindow()
{
InitializeComponent();
gridModel = new GridModel();
this.DataContext = gridModel;
}
private void Browse_Button_Click(object sender, RoutedEventArgs e)
{
WakeupClass clsWakeup = new WakeupClass();
clsWakeup.BrowseFile += new EventHandler(gridModel.ExcelFileOpen);
clsWakeup.Start();
}
void PrintText(object sender, SelectionChangedEventArgs args)
{
//var item = pointsComboBox SelectedItem as Point;
//if(item != null)
//{
// tb.Text = "You selected " + item.name + ".";
//}
MessageBox.Show("I'd like to show the item.name in the TextBox.");
}
}
public class WakeupClass
{
public event EventHandler BrowseFile;
public void Start()
{
BrowseFile(this, EventArgs.Empty);
}
}
}
Record.cs:
namespace XY
{
public class Point : ViewModelBase
{
private string _name;
public string name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("name");
}
}
private int _code;
public int code
{
get { return _code; }
set
{
_code = value;
OnPropertyChanged("code");
}
}
}
}
ViewModelBase.cs:
namespace XY
{
public class Record : ViewModelBase
{
private string _name;
public string name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("name");
}
}
private int _PointCode;
public int PointCode
{
get { return _PointCode; }
set
{
_PointCode = value;
OnPropertyChanged("PointCode");
}
}
private Record _selectedRow;
public Record selectedRow
{
get { return _selectedRow; }
set
{
_selectedRow = value;
OnPropertyChanged("SelectedRow");
}
}
private Point _selectedPoint;
public Point SelectedPoint
{
get { return _selectedPoint; }
set
{
_selectedPoint = value;
_selectedRow.PointCode = _selectedPoint.code;
OnPropertyChanged("SelectedRow");
}
}
}
}
GridModel.cs:
using System.ComponentModel;
namespace XY
{
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
过程如下:
单击“浏览”按钮以加载数据。
单击第一列“通道”以对列表进行排序(这是一个错误,但如果不这样做,则不会显示“点设置”项)。
单击“点设置”组合框以选择项目(point1,point2,...等)。
该代码应该在文本框中显示选定的项目名称。
如果所有内容都在MainWindow.xaml.cs中,则可以访问ComboBox项。由于我将代码分为不同的类,因此无法正常工作。请帮我。任何建议都会有所帮助。
答案 0 :(得分:1)
您的绑定确实起作用。您可以利用sender
对象来实现所需的功能。
void PrintText(object sender, SelectionChangedEventArgs args)
{
var comboBox = sender as ComboBox;
var selectedPoint = comboBox.SelectedItem as Point;
tb.Text = selectedPoint.name;
}
答案 1 :(得分:0)
问题在于DataGridColumn不属于WPF逻辑树的一部分,因此您的相对源绑定将不起作用。使您的工作束缚的唯一方法是一种克鲁格(以我的经验,这在WPF中很常见)。在逻辑树中创建一个虚拟元素,然后对其进行引用。
所以
<FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>
<DataGrid ItemsSource="{Binding channels}"
SelectedItem="{Binding SelectedRow, Mode=TwoWay}"
Margin="0,0,0,-0.2">
然后您的绑定将如下所示
<ComboBox x:Name="piontsComboBox"
ItemsSource="{Binding DataContext.points,
Source={x:Reference dummyElement}}"
SelectionChanged="PrintText"
DisplayMemberPath="name"
SelectedValuePath="name"
Margin="5"
SelectedItem="{Binding DataContext.SelectedPoint,
Source={x:Reference dummyElement},
Mode=TwoWay}"/>