gpt4 book ai didi

c# - 具有自定义类型和插入控件的 WPF ListView 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:15 27 4
gpt4 key购买 nike

我正在制作聊天程序,我已经制作了连接服务器的 API。

这是API的核心类

public partial class PengChat3ClientSock : IDisposable

嗯,PengChat3ClientSock 有一个 Rooms 属性。

public class Room
{
public uint ID { get; private set; }

public string Name { get; private set; }

public string Master { get; internal set; }

public short MaxConnectorNum { get; private set; }

public bool IsNeedPassword { get; private set; }
}

这是一个 Rooms 属性

public Room[] Rooms { get; private set; }

现在是客户端,我有显示房间列表的 ListView。

<ListView x:Name="listView_RoomList" Grid.Row="1" Grid.ColumnSpan="2">
</ListView>

我想像这样设计 ListView 。

RoomName Master MaxConnectorNum Password(If IsNeedPassword is true = Enable) EntryButton   DeleteButton(If Master is I = Enable)
Label Label Label PasswordBox Button Button
Label Label Label PasswordBox Button Button
Label Label Label PasswordBox Button Button
Label Label Label PasswordBox Button Button

顶部是标题。我想插入这样的控件。

如果属性 Rooms 改变了,我想自动改变这个 ListView

是不是太难了?但我认为这是可能的。

感谢您的回答。

最佳答案

如果我没有误解你的问题试试这个

Room.cs

public class Room
{
public uint ID { get; set; }

public string Name { get; set; }

public string Master { get; set; }

public short MaxConnectorNum { get; set; }

public bool IsNeedPassword { get; set; }
}

xaml

    <Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="deleteButtonStyle">
<Setter Property="IsEnabled" Value="False"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Master}" Value="I">
<DataTrigger.Setters>
<Setter Property="IsEnabled" Value="True"></Setter>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListView ItemsSource="{Binding Rooms}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="RoomName">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="Label"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Master">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="Label"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="MaxConnectorNum">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="Label"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Password">
<GridViewColumn.CellTemplate>
<DataTemplate>
<PasswordBox IsEnabled="{Binding IsNeedPassword }" MinWidth="100"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="EntryButton">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Button"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="DeleteButton">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Button" Style="{StaticResource deleteButtonStyle}">
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>

xaml.cs

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext=new ViewModel();
}
}

ViewModel

    public class ViewModel
{
public ObservableCollection<Room> Rooms { get; set; }

public ViewModel()
{
Rooms = new ObservableCollection<Room>()
{
new Room(){Master="I",IsNeedPassword=false},
new Room(){Master="I",IsNeedPassword=true},
new Room(){Master="j",IsNeedPassword=false},
new Room(){Master="k",IsNeedPassword=true}
};
}
}

output enter image description here

关于c# - 具有自定义类型和插入控件的 WPF ListView 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com