2014-09-02 159 views
2

我正在製作聊天程序,並且我已經創建了API來連接服務器。WPF ListView與自定義類型和插入控件的綁定

這是API

public partial class PengChat3ClientSock : IDisposable 

好,PengChat3ClientSockRooms物業的核心類。

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

這是一個房間財產

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

太難了嗎?但我認爲這是可能的。

感謝您的回答。

+0

*是不是太難了?*?不,這是相當標準。 *我想自動更改此ListView * ...是的,我們都希望我們的代碼自己寫,但它不會這樣你就不得不這樣做。你試過什麼了? – Sheridan 2014-09-02 15:07:55

+0

@Sheridan嗯..我嘗試了很多解決方案。如Gri​​dView ..但我無法獲得良好的輸出。現在我是手機。明天我會上傳代碼。 – Lee 2014-09-02 15:19:21

回答

2

如果我沒有誤解你的問題試試這個

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

視圖模型

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

輸出 enter image description here

+2

哇...非常感謝。我想我需要更多的學習。 :) – Lee 2014-09-02 17:46:56