2012-09-23 21 views
0

我是WPF新手。我目前正在開發一個應用程序,我在MSVC#2010中的解決方案有兩個項目。一個項目(即MSPBoardControl)有一個mainwindow.xaml,我添加了另一個wpf窗口ConnectView.xaml。通過在mainwindow.xaml中使用網格,我可以在我的應用程序mainwindow.xaml.cs中添加Connectview.xaml視圖。我的第二個項目是一個具有用戶控件(即電壓)的類庫。無法將用戶控件添加到ListBox中SelectedItem上的現有網格

Mainwindow.xaml:此網格是一個我加我Connectview.xaml UI組件

<Grid Grid.Row="0" Name="MainGrid" HorizontalAlignment="Stretch" Style="{DynamicResource styleBackground}" >       
       </Grid> 

我在向左側mainwindow.xaml一個列表框擁有的產品清單。 (即電壓,時鐘等)。 .xaml的右側有我上面顯示的網格(包含啓動時的connectview)。我基本需要的是,當我單擊列表框中的項目時,我想隱藏啓動時顯示的視圖(connectview),並使selecteditem UI組件可見。

正如我在開始時提到的,我想讓我的classlibrary(VoltageView)的用戶控件可以在列表框的「電壓」項中顯示出來。

列表框在我mainwindow.xaml:

<ListBox Name="ButtonPanel" Style="{DynamicResource styleBanner}" ItemsSource="{Binding BoardOperations, Mode=TwoWay}" SelectedItem="{Binding SelectedList}" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Name="BoardTabChanger" Margin="53,27,0,0" Text="{Binding ListName}" /> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

MSPBoardControl的ViewModel類是在這裏:

public class BoardControlViewModel : INotifyPropertyChanged 
{   
    public ObservableCollection<BoardControlModel> m_BoardOperation; 
    public List<ConnectModel> m_BoardNames; 

    public BoardControlViewModel() 
    { 
     //Adding items to ListBox 
    } 

    public List<ConnectModel> BoardNames 
    { 
     get; set; 
    } 

    private ConnectModel m_SelectedBoardItem; 
    public ConnectModel SelectedBoard 
    { 
     get; set; 
    } 

    public ObservableCollection<BoardControlModel> BoardOperations 
    { 
     get; set; 
    } 

    //GIVES ME THE SELECTED ITEM FROM LISTBOX 

    private BoardControlModel m_SelectedListItem; 
    public BoardControlModel SelectedList 
    { 
     get 
     { 
      return m_SelectedListItem; 
     } 

     set 
     { 
      m_SelectedListItem = value; 
      onListSelected(value); 
      NotifyPropertyChanged("SelectedList");     
     } 
    } 

    private void onListSelected(BoardControlModel SelectedItem) 
    { 
     if (SelectedItem.ListName == "Voltage") 
     { 
      //HOW CAN I ADD THE VOLTAGEVIEW HERE??? 
     } 
    } 

OnListSelected()上述方法檢索selectedItem屬性和當我檢查條件dat item = voltage,我想將voltageview組件添加到我的maingrid並隱藏connectview。

VoltageView.xaml:

<Grid Name="VoltageControl" Style="{DynamicResource styleBackground}" DataContext="{StaticResource VoltageViewModel}" > 
    <Label Content="Label" FontSize="15" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="10,31,0,0" Name="label9" VerticalAlignment="Top" Width="117" /> 
    <Label Content="Set Voltage" FontSize="15" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="150,31,0,0" Name="label10" VerticalAlignment="Top" Width="117" /> 
    <Label Content="Current Value" FontSize="16" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="300,31,0,0" Name="label11" VerticalAlignment="Top" Width="117" /> 
    <Label Content="Enable/Disable" FontSize="15" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="460,31,0,0" Name="label12" VerticalAlignment="Top" Width="127" /> 
    <Button Content="Refresh All" FontSize="13" Height="25" HorizontalAlignment="Left" Margin="230,520,0,0" Name="RefreshBtn" VerticalAlignment="Top" Width="105" /> 
</Grid>  

請幫助!

回答

0

我打算保留這個將軍(ish)。您最好的選擇可能是在Grid類型的BoardControlViewModel上添加屬性(或者電壓和連接常見的任何UIElement)。隨着選擇更改,將UI屬性更改爲所需的視圖,併爲該屬性創建NotifyPropertyChanged。如果你喜歡,你可以從onListSelected右邊調用NotifyPropertyChanged。將你正在使用的任何網格的內容綁定到該屬性,最好在xaml中正確使用。當屬性發生變化時,WPF會通知網格它的綁定內容已經改變,它會查詢你的新屬性,看看它應該是什麼。

+0

謝謝雅各布。你可以用示例代碼證明它嗎? :) –

+0

不容易,這是關於你的問題的事情:這是相當*大*。 –

+0

^我把它縮短了。我只想隱藏Connectview並在我從列表框中選擇電壓時添加電壓查看。檢查OnListSelected()方法。那是我想隱藏/顯示視圖並調用notifypropertychanged。我現在已經確定了它,並且希望它能夠說清楚。讓我知道是否有其他事情需要改變 –

相關問題