第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

XAML 單擊時(shí)切換 ListViewItem 可見性

XAML 單擊時(shí)切換 ListViewItem 可見性

C#
拉風(fēng)的咖菲貓 2023-12-17 20:05:39
我正在嘗試根據(jù)所選的 ListViewItem 更改項(xiàng)目可見性。基本上,ListView 中的每一列在網(wǎng)格上都有兩個(gè)項(xiàng)目、一個(gè)標(biāo)簽和一個(gè)控件(組合框、日期選擇器、文本框等)。如果選擇 ListViewItem,那么我希望該行中的所有控件都可見,否則標(biāo)簽應(yīng)該可見。這是在 UserControl 上,而不是在 Window 上,如果這有什么區(qū)別的話。這是我的視圖模型    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貢獻(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>


查看完整回答
反對 回復(fù) 2023-12-17
  • 1 回答
  • 0 關(guān)注
  • 183 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號