我在HeaderedContentControl的Header中有一个按钮当我点击按钮时命令正常触发,但由于实际的listboxitem没有被选中,我没有数据可以使用。当选择子控件或首先与子控件交互时,如何选择HeaderedContentControl?
这是我的控件
<ListBox x:Name="SearchList"
Grid.Row="1"
Grid.Column="0"
Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Path=SearchResults}"
SelectedItem="{Binding Path=SelectedResult,Mode=TwoWay,Converter={StaticResource DebugConverter}}"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="600"
Margin="20,2,0,0"
Style="{StaticResource BorderStyle}">
<sb:BindingHelper.Binding>
<sb:BindingList>
<sb:RelativeSourceBinding TargetProperty="Background"
RelativeMode="FindAncestor"
AncestorType="ListBoxItem"
Converter="{StaticResource DebugConverter}"
ConverterParameter="AlternatingRows" />
</sb:BindingList>
</sb:BindingHelper.Binding>
<toolkit:HeaderedContentControl IsTabStop="True">
<toolkit:HeaderedContentControl.Header>
<Grid Width="575"
Margin="5,5,10,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0"
Grid.Row="0"
Text="{Binding Path=PatientName}"
HorizontalAlignment="Left"
Margin="0" />
<Button Grid.Column="3"
Grid.Row="0"
Content="Complete Survey"
IsTabStop="False">
<sb:BindingHelper.Binding>
<sb:BindingList>
<sb:RelativeSourceBinding Path="DataContext.OpenSurveyCommand"
TargetProperty="Command"
RelativeMode="FindAncestor"
AncestorType="ListBox"
Converter="{StaticResource DebugConverter}"
ConverterParameter="SurveyCommandButton" />
<sb:RelativeSourceBinding Path="DataContext.HasFocus"
TargetProperty="Focus" />
</sb:BindingList>
</sb:BindingHelper.Binding>
</Button>
</Grid>
</toolkit:HeaderedContentControl.Header>
<sb:BindingHelper.Binding>
<sb:RelativeSourceBinding TargetProperty="IsExpanded"
Path="IsSelected"
RelativeMode="FindAncestor"
AncestorType="ListBoxItem"
BindingMode="TwoWay" />
</sb:BindingHelper.Binding>
<Grid Margin="5,2,0,2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="275" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="{Binding MRN,Converter={StaticResource StringLabelConverter},ConverterParameter=MRN}" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Style="{StaticResource SearchResultsTextBlockStyle }"
Text="{Binding OriginalVisitNumber,Converter={StaticResource StringLabelConverter},ConverterParameter='Original Visit Number'}" />
<TextBlock Grid.Row="2"
Grid.Column="0"
Style="{StaticResource SearchResultsTextBlockStyle }"
Text="{Binding OriginalAdmitDate,Converter={StaticResource StringLabelConverter},ConverterParameter='Original Admit Date'}" />
<TextBlock Grid.Row="3"
Grid.Column="0"
Style="{StaticResource SearchResultsTextBlockStyle }"
Text="{Binding OriginalReason,Converter={StaticResource StringLabelConverter},ConverterParameter='Original Reason'}" />
<TextBlock Grid.Row="1"
Grid.Column="1"
Style="{StaticResource SearchResultsTextBlockStyle }"
Text="{Binding ReAdmitVisitNumber,Converter={StaticResource StringLabelConverter},ConverterParameter='ReAdmit Visit Number'}" />
<TextBlock Grid.Row="2"
Grid.Column="1"
Style="{StaticResource SearchResultsTextBlockStyle }"
Text="{Binding ReAdmitDate,Converter={StaticResource StringLabelConverter},ConverterParameter='Readmit Date'}" />
<TextBlock Grid.Row="3"
Grid.Column="1"
Style="{StaticResource SearchResultsTextBlockStyle }"
Text="{Binding ReAdmitReason,Converter={StaticResource StringLabelConverter},ConverterParameter='ReAdmit Reason'}" />
</Grid>
</toolkit:HeaderedContentControl>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:0)
实际修复涉及一些代码改组,我将实际结果列表对象移动到viewModel的集合中,并将button命令的代码放在该viewModel而不是父VM中。这样可以提供更大的灵活性。
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using WavelengthIS.Silverlight.Core.Helpers;
namespace ReadmissionTrackingApplication.Client.ViewModel
{
/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm/getstarted
/// </para>
/// </summary>
public partial class ReadmitPatientListViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the ReadmitPatientListViewModel class.
/// </summary>
////public override void Cleanup()
////{
//// // Clean own resources if needed
//// base.Cleanup();
////}
#region Declarations
ICommand _openSurveyCommand;
Messenger _messenger = Messenger.Default;
#endregion
#region Command Properties
public ICommand OpenSurveyCommand
{
get
{
if (_openSurveyCommand == null)
{
_openSurveyCommand = new RelayCommand(() => OnSurveyCommandExecute());
}
return _openSurveyCommand;
}
private set { }
}
#endregion
#region Command Methods
private void OnSurveyCommandExecute()
{
Wait.Begin("Loading Patient List...");
_messenger.Send<ReadmitPatientListViewModel>(this);
_messenger.Send<Messages.NavigationRequest<SubClasses.URI.PageURI>>(GetNavRequest_QUESTIONAIRRESHELL());
}
#endregion
#region Properties
#endregion
private static Messages.NavigationRequest<SubClasses.URI.PageURI> GetNavRequest_QUESTIONAIRRESHELL()
{
Messages.NavigationRequest<SubClasses.URI.PageURI> navRequest =
new Messages.NavigationRequest<SubClasses.URI.PageURI>(
new SubClasses.URI.PageURI(Helpers.PageLinks.QUESTIONAIRRESHELL, System.UriKind.Relative));
return navRequest;
}
partial void OnCreated()
{
}
}
}