亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

XAML 單擊時切換 ListViewItem 可見性

XAML 單擊時切換 ListViewItem 可見性

C#
拉風的咖菲貓 2023-12-17 20:05:39
我正在嘗試根據所選的 ListViewItem 更改項目可見性。基本上,ListView 中的每一列在網格上都有兩個項目、一個標簽和一個控件(組合框、日期選擇器、文本框等)。如果選擇 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 =>                   }                  ListViewTest.DataContext = this;                  ListViewTest.ItemsSource = DailyServiceLogs;                }我嘗試過使用 DataTriggers,但我對它們不太熟悉
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

保持 SelectedLog 更新的正確方法是為 ListView 創建一個 ItemContainerStyle 并將其綁定在那里。這是正確的解決方案。您需要使項目視圖模型成為實現 INotifyPropertyChanged 的實際視圖模型。事實上,更改該屬性永遠不會影響 UI 中的任何內容,因為 UI 不會收到更改通知。該解決方案如下。


但是,如果視圖模型并不特別關心它是否被選中,我們可以刪除您的 SelectionChanged 處理程序,從項目類中刪除 SelectedLog,然后直接綁定到觸發器中 ListViewItem 的實際 IsSelected 屬性。請注意,您將組合框設置為在選擇項目時可見,但在未選擇項目時從未隱藏它。我已經解決了這個問題。當我們被選中時,一個控件會隱藏,而當我們未被選中時,另一個控件會隱藏。


<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>

您應該學習如何創建和綁定視圖模型屬性,這就是該解決方案。首先,我們需要創建“viewmodel”實際的視圖模型:


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);

        }

    }

}

其次,添加一個 ItemContainerStyle,用于在選擇項目時設置 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>


查看完整回答
反對 回復 2023-12-17
  • 1 回答
  • 0 關注
  • 187 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號