我在基于其他属性的数据绑定方面遇到问题。我的组合框工作正常,但数据网格没有。我正试图将窗口绑定到代码隐藏使用
DataContext="{Binding RelativeSource={RelativeSource Self}}"
所以我可以设计时间智能感知,但我有点使用x:Name:"_Window"
HRRM的模型有很多实体,我知道我正在尝试绑定的属性也拼写正确等等。但我无法弄清楚为什么DataGrid不会显示员工。我还确保将数据放在可观察集合中,并且我已经尝试将项目源绑定到ListEmployees和SmsEmployees,并且我尝试过的任何工作都没有。这是我尝试过的最后一段代码。
<Window x:Class="GUI.Employees.Misc.SendSms"
x:Name="_Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Send SMS" Height="525" Width="1000" DataContext="{Binding ElementName=_Window}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="270*" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="270*" />
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0" x:Name="cmbCompany" DisplayMemberPath="Name" ItemsSource="{Binding Path=Companies, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Margin="10,10,10,0" VerticalAlignment="Top" SelectionChanged="cmbCompany_SelectionChanged"/>
<DataGrid Grid.Column="0" x:Name="dgEmployees" Margin="10,37,10,10" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Path=ListEmployees, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window} }">
<DataGrid.Columns>
<DataGridTextColumn Header="Company" Binding="{Binding HRCo }"/>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Craft" Binding="{Binding StdCraft}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dgSendSms" Margin="10,37,10,10" IsReadOnly="True" Grid.Column="2" ItemsSource="{Binding SmsEmployees, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window} }">
<DataGrid.Columns>
<DataGridTextColumn Header="Company" Binding="{Binding HRCo}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Craft" Binding="{Binding StdCraft}"/>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="cmdMoveAllHired" Content=">>" Margin="10,85,10,0" VerticalAlignment="Top" Grid.Column="1"
Click="cmdMoveAllHired_Click" />
<Button x:Name="cmdReturnSingleItem" Content=">" Margin="10,122,10,0" VerticalAlignment="Top"
Grid.Column="1" Click="cmdReturnSingleItem_Click" />
<Button x:Name="cmdMoveAllReturned" Content="<<" Margin="10,196,10,0" VerticalAlignment="Top"
Grid.Column="1" Click="cmdMoveAllReturned_Click" />
<Button x:Name="cmdHireSingleItem" Content="<" Margin="10,159,10,0" VerticalAlignment="Top" Grid.Column="1"
Click="cmdHireSingleItem_Click" />
<Button x:Name="cmdGenerate" Content="Generate" Grid.Column="1" Margin="10,0,10,10"
VerticalAlignment="Bottom" Click="cmdGenerate_Click" Visibility="Collapsed" />
<Button x:Name="cmdBack" Content="Back" Grid.Column="1" HorizontalAlignment="Left" Margin="10,249,0,0"
VerticalAlignment="Top" Width="130" Click="cmdBack_Click" />
<Button x:Name="cmdSendSMS" Content="Send SMS" Grid.Column="1" HorizontalAlignment="Left" Margin="10,316,0,0" VerticalAlignment="Top" Width="130" Click="cmdSendSMS_Click"/>
</Grid>
namespace GUI.Employees.Misc
{
public partial class SendSms
{
public ObservableCollection<HQCO> Companies { get; set; }
public ObservableCollection<HRRM> ListEmployees { get; set; }
private ObservableCollection<HRRM> _smsEmployees;
public ObservableCollection<HRRM> SmsEmployees {
get { return _smsEmployees; }
set { _smsEmployees = value;
}
}
public SendSms()
{
InitializeComponent();
Companies = new ObservableCollection<HQCO>(HQCO.GetActivePRCompanies());
Companies.Insert(0, new HQCO { HQCo = 0, Name = "All" });
// cmbCompany.SelectedItem = _companies.Single(x=>x.HQCo == 0);
SmsEmployees = new ObservableCollection<HRRM>();
ChangeCompany();
}
private void ChangeCompany()
{
if (((HQCO)cmbCompany.SelectedItem)?.HQCo == 0)
foreach (var co in Companies)
co.IsChecked = true;
else
foreach (var co in Companies)
if (((HQCO)cmbCompany.SelectedItem) == co)
co.IsChecked = true;
else
co.IsChecked = false;
ListEmployees = new ObservableCollection<HRRM>(Facade.GetEmployeePhoneNumbers(Companies.ToList(), false).OrderBy(x => x.SortName));
}
}
答案 0 :(得分:1)
我不相信您的ListEmployees
ItemsSource
会更新DataGrid
,因为它不是DependencyProperty
或您尚未实施INotifyPropertyChanged
。它没有更新,因为您正在为属性分配一个新实例。
而是尝试将其添加到构造函数中:
ListEmployees = new ObservableCollection<HRRM>();
这是你的ChangeCompany方法:
foreach (var employee in Facade.GetEmployeePhoneNumbers(Companies.ToList(), false).OrderBy(x => x.SortName))
{
ListEmployees.Add(employee);
}
以上实际上会使用ObservableCollection
,然后它应该更新UI。
答案 1 :(得分:0)
我认为您的问题是如何分配Window的DataContext。在您的XAML中,添加对命名空间的引用,以便Window可以找到您的SendSms视图模型,然后在XAML中分配DataContext属性(见下文),或者在Window的代码隐藏中。如果以这种方式设置DataContext,则在绑定到SendSms视图模型时不需要使用RelativeSource,但是如果要查看更改,还需要确保使用implements INotifyPropertyChanged填充可观察集合的类。
<Window x:Name="_Window"
xmlns:local="clr-namespace:GUI.Employees.Misc" >
<!-- set the DataContext of this Window to an instance of SendSms -->
<Window.DataContext>
<local:SendSms />
</Window.DataContext>
<DataGrid ItemsSource="{Binding ListEmployees}">
...
</DataGrid>