【发布时间】:2019-09-20 16:14:55
【问题描述】:
我正在尝试根据所选的 ListViewItem 更改项目的可见性。基本上,ListView 中的每一列在网格上都有两个项目,一个标签和一个控件(Combobox、DatePicker、Textbox 等)。如果选择了 ListViewItem,那么我希望该行中的所有控件都可见,否则标签应该可见。这是在 UserControl 上,而不是在 Window 上,如果这有什么不同的话。
这是我的视图模型
public class DailyServiceLogsViewModel
{
public int DailyServiceLogID { get; set; }
public int EmployeePositionID { get; set; }
public PositionType SelectedEmployeePosition { get; set; }
public List<PositionType> EmployeePositionList { get; set; }
public List<EmployeeSelectionListViewModel> EmployeeList { get; set; }
public EmployeeSelectionListViewModel SelectedEmployee { get; set; }
public string EmployeeName { get; set; }
public string PositionDescription { get; set; }
public DateTime? Date { get; set; }
public string WorkArea { get; set; }
public bool SelectedLog { get; set; }
}
后面的代码
private DBContext _dbContext= new DBContext();
public ObservableCollection<DailyServiceLogsViewModel> DailyServiceLogs { get; set; }
public void OnLoad()
{
_dbContext= new DBContext();
List<EmployeeSelectionListViewModel> employeeList = _dbContext.Employees.Where(emp => emp.Active).Select(employee => new EmployeeSelectionListViewModel { EmployeeID = employee.EmployeeID, EmployeeName = employee.FirstName + " " + employee.LastName }).ToList();
DailyServiceLogs = new ObservableCollection<DailyServiceLogsViewModel>();
foreach (var serviceLog in _dbContext.DailyServiceLogs.Where(d => d.PayPeriodID == CurrentPayPeriod.PayPeriodID).OrderBy(d => d.EmployeePosition.Employee.LastName).ThenBy(d => d.EmployeePosition.Employee.FirstName))
{
DailyServiceLogs.Add(new DailyServiceLogsViewModel
{
DailyServiceLogID = serviceLog.DailyServiceLogID,
EmployeePositionID = serviceLog.EmployeePositionID,
SelectedEmployeePosition = serviceLog.EmployeePosition.PositionType,
EmployeeName = serviceLog.EmployeePosition.Employee.FirstName + " " + serviceLog.EmployeePosition.Employee.LastName,
Date = serviceLog.Date,
PositionDescription = serviceLog.EmployeePosition.PositionType.Description,
WorkArea = serviceLog.Workarea,
EmployeeList = employeeList,
SelectedEmployee = new EmployeeSelectionListViewModel { EmployeeID = serviceLog.EmployeePosition.EmployeeID, EmployeeName = serviceLog.EmployeePosition.Employee.FirstName + " " + serviceLog.EmployeePosition.Employee.LastName },
EmployeePositionList = _dbContext.PositionTypes.Where(pt => pt.Active && _payrollContext.EmployeePositions.Any(ep => ep.EmployeeID == serviceLog.EmployeePosition.EmployeeID && ep.PositionTypeID == pt.PositionTypeID)).ToList(),
SelectedLog = false
});
}
ListViewTest.DataContext = this;
ListViewTest.ItemsSource = DailyServiceLogs;
}
private void DailyServiceLog_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(sender is ListView senderListView)) return;
if (senderListView.SelectedItem == null) return;
if (senderListView.SelectedItem.GetType() == typeof(DailyServiceLogsViewModel))
{
foreach (var log in DailyServiceLogs)
{
log.SelectedLog = log.DailyServiceLogID == ((DailyServiceLogsViewModel) senderListView.SelectedItem).DailyServiceLogID;
}
}
}
我尝试过使用 DataTriggers,但我对它们不太熟悉
<ListView Name="ListViewTest" Grid.Row="1" ItemsSource="{Binding}" SelectionChanged="DailyServiceLog_OnSelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn x:Name="clmServiceEmployeeName" Header="Employee" Width="155">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="gdEmployee" Width="{Binding ElementName=clmServiceEmployeeName, Path=Width}" Tag="{Binding DailyServiceLogID}">
<Label Content="{Binding EmployeeName}" >
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem.SelectedLog}" Value="True">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
<ComboBox Tag="{Binding ElementName=gdEmployee, Path=Tag}" ItemsSource="{Binding EmployeeList}" SelectedValue="{Binding SelectedEmployee.EmployeeID}" DisplayMemberPath="EmployeeName" SelectedValuePath="EmployeeID" FlowDirection="LeftToRight" Margin="15,5" HorizontalAlignment="Stretch" >
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem.SelectedLog}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
...
</GridView>
</ListView.View>
</ListView>
我也试过转换器
<ListView Name="ListViewTest" Grid.Row="1" ItemsSource="{Binding}" SelectionChanged="DailyServiceLog_OnSelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn x:Name="clmServiceEmployeeName" Header="Employee" Width="155">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="gdEmployee" Width="{Binding ElementName=clmServiceEmployeeName, Path=Width}" Tag="{Binding DailyServiceLogID}">
<Grid.Resources>
<dataconverter:BoolVisibilityConverter x:Key="BoolVisibilityConverter"/>
<dataconverter:InvertedBoolVisibilityConverter x:Key="InvertedBoolVisibilityConverter"/>
</Grid.Resources>
<Label Content="{Binding EmployeeName}" Visibility="{Binding ElementName=PayrollControl, Path=SelectedServiceLog, Converter={StaticResource InvertedBoolVisibilityConverter}}"/>
<ComboBox Tag="{Binding ElementName=gdEmployee, Path=Tag}" ItemsSource="{Binding EmployeeList}" SelectedValue="{Binding SelectedEmployee.EmployeeID}" Visibility="{Binding SelectedServiceLog, Converter={StaticResource BoolVisibilityConverter}}" DisplayMemberPath="EmployeeName" SelectedValuePath="EmployeeID" FlowDirection="LeftToRight" Margin="15,5" HorizontalAlignment="Stretch" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
知道如何让它发挥作用,还是有更好的方法来实现我想要实现的目标?任何帮助将不胜感激。
【问题讨论】:
-
如果您希望标签始终可见,为什么要为您的标签设置可见性?