2013-04-02 25 views
1

我設法建立一個自定義列表框,其中每個項目從一個數據庫中加載顯示在一個堆棧面板中的名稱和姓氏。在這兩個文本框之後,應該有一個按鈕,該按鈕正確綁定到ViewModel的ICommand。 按鈕正確調用正確的方法,但不會刪除selectedPerson,因爲該對象爲空。WPF自定義列表框selectdItem與按鈕

這是列表框

<Style x:Key="CustomHorizontalListbox" TargetType="{x:Type ListBox}"> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Border BorderBrush="Black" BorderThickness="2"> 
       <StackPanel Orientation="Horizontal" Width="200" Margin="0,0,0,0"> 
        <TextBox Text="{Binding FirstName}" Width="60" BorderThickness="0" Margin="0" IsReadOnly="True"></TextBox> 
        <TextBox Text="{Binding LastName}" Width="100" BorderThickness="0" Margin="0" IsReadOnly="True"></TextBox> 
        <Button Width="20" Height="20" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext.OnDeletePatient}" CommandParameter="{Binding}"></Button> 
       </StackPanel> 
       </Border> 
      </DataTemplate> 
     </Setter.Value>     
    </Setter> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <WrapPanel></WrapPanel> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

這是在視圖模型

private void DeletePatient() 
    { 
     patientsManager.DeletePatient(SelectedPatient); 
     ListOfPatients = new ObservableCollection<RealPatient>(patientsManager.GetAllRealPatients()); 
     SelectedPatient = null; 
    } 

相關方法的WPF這是自定義列表框如何被包含在查看

<ListBox Grid.Row="2" Grid.Column="3" Style="{StaticResource CustomHorizontalListbox}" ItemsSource="{Binding ListOfPatients}" SelectedItem="{Binding SelectedPatient}"> 

    </ListBox> 

所以問題在於DeleteMethod中的斷點顯示SelectedPatient = null ..

我怎麼錯過......甚至當我的列表中的項目,而不是單一的按鈕點擊SelectedPatient不會改變

感謝

+0

嘗試增加'模式= TwoWay'和'UpdateSourceTrigger = PropertyChanged'到您的SelectedItem綁定。 –

+0

沒有...它不工作... –

+0

試試我的解決方案,它工作正常,我.. –

回答

0

這裏我的全樣本應用程序,它的工作原理:

<Window.Resources> 
    <Style x:Key="CustomHorizontalListbox" TargetType="{x:Type ListBox}"> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> 
     <Setter Property="BorderThickness" Value="0" /> 
     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <Border BorderBrush="Black" BorderThickness="2"> 
         <StackPanel Orientation="Horizontal" Width="200" Margin="0,0,0,0"> 
          <TextBox Text="{Binding FirstName}" Width="60" BorderThickness="0" Margin="0" IsReadOnly="True"></TextBox> 
          <TextBox Text="{Binding LastName}" Width="100" BorderThickness="0" Margin="0" IsReadOnly="True"></TextBox> 
          <Button Width="20" Height="20" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext.OnDeletePatient}" CommandParameter="{Binding}"></Button> 
         </StackPanel> 
        </Border> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="ItemsPanel"> 
      <Setter.Value> 
       <ItemsPanelTemplate> 
        <WrapPanel></WrapPanel> 
       </ItemsPanelTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Style.Resources> 
      <Storyboard x:Key="OnGotKeyboardFocus1"> 
       <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Selector.IsSelected)"> 
        <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/> 
       </BooleanAnimationUsingKeyFrames> 
      </Storyboard> 
     </Style.Resources> 
     <Style.Triggers> 
      <EventTrigger RoutedEvent="Keyboard.GotKeyboardFocus"> 
       <BeginStoryboard Storyboard="{StaticResource OnGotKeyboardFocus1}"/> 
      </EventTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Window.DataContext> 
    <Regions:ViewModel/> 
</Window.DataContext> 
<Grid> 
    <ListBox Style="{StaticResource CustomHorizontalListbox}" ItemsSource="{Binding ListOfPatients}" SelectedItem="{Binding SelectedPatient}" > 

    </ListBox> 
</Grid> 

這裏我的視圖模型SelectedPatient屬性的定義:

private Patient _SelectedPatient; 
    public Patient SelectedPatient 
    { 
     get { return _SelectedPatient; } 
     set 
     { 
      _SelectedPatient = value; 
      NotifyPropertyChanged(m => m.SelectedPatient); 
     } 
    } 

只要您在ListBox項目行的任意位置單擊,就會設置您選擇的項目。

我希望對此有更深入的解釋,但是我在MSDN論壇上發現它: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1642c5f9-e731-4a3d-9eed-0f574d90d925我使用了適用於我正在使用的應用程序的修復程序。

+0

它運作良好,但如果有人請這個解釋可以某處寫,或提供一些有用的鏈接?...... –

+0

它是基於事件觸發設置屬性的一個時髦的方式。下面是我的理解是,當這個ListBoxItem中檢測到鍵盤焦點事件('GotKeyboardFocus'),然後開始一個動畫當它完成,將設置你的'TargetProperty = Selector.IsSelected'爲true。類似的帖子:http://stackoverflow.com/questions/942548/setting-a-property-with-an-eventtrigger –