我在使用TokenLookUpEdit中选择的值来填充datagrid时遇到问题,其中发票编号= TokenLookUpEdit选择的值
这是我的代码:
using (MySqlConnection cn = new MySqlConnection() { ConnectionString = "DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8" })
{
using (MySqlCommand cmd = new MySqlCommand()
{
Connection = cn,
CommandText = "SELECT InvoiceNumber,ItemCode,ItemName,Items_QTY,Reservation_Date, " +
"EngName,UserName,Address,InvoiceStore,ContsrType,Place FROM Delta_Invoices_Grid where InvoiceNumber ='"+ InvoicesNumbersCombo.Text + "'"
})
{
cn.Open();
MySqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
InvoicesDetails_Grid.ItemsSource = dt.DefaultView;
cn.Close();
}
}
现在的问题是,datagrid仅使用TokenLookUpEdit中的一个值,而不是所有选定值,我从昨天开始已经进行了搜索,但找不到解决方案
这是TokenLookUpEdit和Datagrid的XAML代码:
<GroupBox Header="أرقام الفواتير" Margin="76,10,592,439" FlowDirection="RightToLeft" FontFamily="Droid Arabic Kufi">
<dxg:LookUpEdit x:Name="InvoicesNumbersCombo" Margin="16,12,16,16"
ItemsSource="{Binding Delta_Invoices_Grid}"
EditValue="{Binding InvoiceNumber, Mode=OneWay}" AutomationProperties.IsColumnHeader="True"
>
<dxg:LookUpEdit.StyleSettings>
<dxg:TokenLookUpEditStyleSettings />
</dxg:LookUpEdit.StyleSettings>
</dxg:LookUpEdit>
</GroupBox>
<DataGrid x:Name="InvoicesDetails_Grid" FlowDirection="RightToLeft" IsReadOnly="True" SelectionUnit="FullRow" AutoGenerateColumns="False" CanUserAddRows="True" ItemsSource="{Binding Dawam_Grid_ItemsBinding}" Margin="460,129,10,243">
<DataGrid.Resources>
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#464646" />
<Setter Property="Foreground" Value="#F0F0F0" />
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Foreground" Value="#F88E1D"/>
</Style>
<Style TargetType="{x:Type ScrollBar}" >
<Setter Property="Background" Value="#FAFAFA" />
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#360E05"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#464646" Offset="0.221"/>
<GradientStop Color="#4E93CA" Offset="1"/>
</LinearGradientBrush>
</DataGrid.Background>
<DataGrid.RowBackground >
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Transparent" Offset="0.221"/>
<GradientStop Color="Transparent" Offset="1"/>
</LinearGradientBrush>
</DataGrid.RowBackground>
<DataGrid.Columns>
<DataGridTextColumn Header="رقم الفاتورة" x:Name="InvoiceNumber" IsReadOnly="True" Binding="{Binding InvoiceNumber}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="كود الصنف" x:Name="ItemCode" IsReadOnly="True" Binding="{Binding ItemCode}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="إسم الصنف" x:Name="ItemName" Width="250" IsReadOnly="True" Binding="{Binding ItemName}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="الكمية" x:Name="Items_QTY" IsReadOnly="True" Binding="{Binding Items_QTY}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="تاريخ التركيب" x:Name="Reservation_Date" IsReadOnly="True" Binding="{Binding Reservation_Date, StringFormat=dd/MM/yyyy}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="إسم المهندس" x:Name="EngName" Width="70" IsReadOnly="True" Binding="{Binding EngName}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="إسم البائع" x:Name="UserName" IsReadOnly="True" Binding="{Binding UserName}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="العنوان" x:Name="Address" Width="70" IsReadOnly="True" Binding="{Binding Address}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="المعرض" x:Name="InvoiceStore" IsReadOnly="True" Binding="{Binding InvoiceStore}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="نوع التركيب" x:Name="ContsrType" Width="70" IsReadOnly="True" Binding="{Binding ContsrType}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="مكان التركيب" x:Name="Place" Width="70" IsReadOnly="True" Binding="{Binding Place}" Foreground="#F0F0F0"/>
<DataGridTextColumn Header="الفني" x:Name="TechName" Width="70" IsReadOnly="False" Binding="{Binding TechName}" Foreground="#F0F0F0"/>
</DataGrid.Columns>
</DataGrid>
这是TokenLookUpEdit填充代码:
InitializeComponent();
using (MySqlConnection cn = new MySqlConnection() { ConnectionString = "DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8" })
{
using (MySqlCommand cmd = new MySqlCommand() { Connection = cn, CommandText = "SELECT InvoiceNumber,CustomerName FROM Delta_Invoices_Grid " })
{
cn.Open();
MySqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
InvoicesNumbersCombo.ItemsSource = dt.DefaultView;
InvoicesNumbersCombo.ValueMember = "CustomerName";
InvoicesNumbersCombo.DisplayMember = "InvoiceNumber";
}
}
最佳答案
您需要做的第一件事是将EditValue
绑定到集合,因为您将获得行列表并使用CollectionConverter
,并且您需要维护ObservableCollection
的集合更改事件,以便您将获得选定的行
Xaml
`EditValue="{Binding SelectedRows, Converter={local:CollectionConverter}}`
转换器
public class CollectionConverter : MarkupExtension, IValueConverter {
public CollectionConverter() { }
public override object ProvideValue(IServiceProvider serviceProvider) {
return this;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return ((ObservableCollection<Object>)value).ToList<object>();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return new ObservableCollection<Object>((List<object>)value);
}
}
支柱
public ObservableCollection<Order> Orders { get; private set; }
ObservableCollection<Object> _SelectedRows;
public ObservableCollection<Object> SelectedRows {
get { return _SelectedRows; }
set {
_SelectedRows.CollectionChanged -= OnSelectedRowsCollectionChanged;
_SelectedRows.Clear();
foreach (var item in value)
_SelectedRows.Add(item);
_SelectedRows.CollectionChanged += OnSelectedRowsCollectionChanged;
}
}
CTOR
_SelectedRows = new ObservableCollection<object>();
_SelectedRows.CollectionChanged += OnSelectedRowsCollectionChanged;
事件
void OnSelectedRowsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
NotifyPropertyChanged("SelectedRows");