1 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
保持 SelectedLog 更新的正確方法是為 ListView 創(chuàng)建一個(gè) ItemContainerStyle 并將其綁定在那里。這是正確的解決方案。您需要使項(xiàng)目視圖模型成為實(shí)現(xiàn) INotifyPropertyChanged 的實(shí)際視圖模型。事實(shí)上,更改該屬性永遠(yuǎn)不會(huì)影響 UI 中的任何內(nèi)容,因?yàn)?UI 不會(huì)收到更改通知。該解決方案如下。
但是,如果視圖模型并不特別關(guān)心它是否被選中,我們可以刪除您的 SelectionChanged 處理程序,從項(xiàng)目類中刪除 SelectedLog,然后直接綁定到觸發(fā)器中 ListViewItem 的實(shí)際 IsSelected 屬性。請注意,您將組合框設(shè)置為在選擇項(xiàng)目時(shí)可見,但在未選擇項(xiàng)目時(shí)從未隱藏它。我已經(jīng)解決了這個(gè)問題。當(dāng)我們被選中時(shí),一個(gè)控件會(huì)隱藏,而當(dāng)我們未被選中時(shí),另一個(gè)控件會(huì)隱藏。
<Label Content="{Binding EmployeeName}" >
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem}}" 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 IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem}}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
您應(yīng)該學(xué)習(xí)如何創(chuàng)建和綁定視圖模型屬性,這就是該解決方案。首先,我們需要?jiǎng)?chuàng)建“viewmodel”實(shí)際的視圖模型:
public class DailyServiceLogsViewModel : ViewModelBase
{
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; }
// Only properties like this will notify the UI when they update.
private bool _isSelectedLog = false;
public bool IsSelectedLog
{
get => _isSelectedLog;
set => SetProperty(ref _isSelectedLog, value);
}
}
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
public void SetProperty<T>(ref T field, T value, [CallerMemberName] string propName = null)
{
if (!Object.Equals(field, value))
{
field = value;
OnPropertyChanged(propName);
}
}
}
其次,添加一個(gè) ItemContainerStyle,用于在選擇項(xiàng)目時(shí)設(shè)置 IsSelectedLog。您可以刪除 SelectionChanged 處理程序。
<ListView ItemsSource="{Binding Items}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelectedLog}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn x:Name="clmServiceEmployeeName" Header="Employee" Width="155">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Tag="{Binding DailyServiceLogID}">
<Label Content="{Binding EmployeeName}" >
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding 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 SelectedLog}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
- 1 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報(bào)