Listbox简单用法

<ListBox x:Name="ListBoxPatientAllergy" Grid.Row="1"
ItemContainerStyle="{StaticResource ListboxStyle}"
Background="Transparent" BorderThickness="0"
Foreground="Black"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.PanningMode="None"
ItemsSource="{Binding Path=PatientAllergyEntityList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<sync:DateTimeEdit x:Name="dpPatientAllergy"
Height="28" Width="100" Margin="0,0,0,5"
DateTime="{Binding Path=Date, Mode=TwoWay}"
CustomPattern="dd MMM yyyy"
MinDateTime="01 Jan 1800"/>

<TextBox Name="TxtPatientAllergy" Grid.Column="1"
HorizontalAlignment="Stretch" Height="28"
MaxLength="100" Margin="2,0,0,5" VerticalContentAlignment="Center"
Validation.Error="Validation_Error"
Text="{Binding Path=Description,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"/>

<ComboBox Name="CmbPatientAllergy" Grid.Column="2" VerticalContentAlignment="Center"
ItemContainerStyle="{StaticResource ComboBoxItemStyle}"
Background="White" Margin="2,0,0,5" Height="28"
ItemsSource="{Binding RelativeSource={RelativeSource Findancestor, AncestorType={x:Type UserControl}}, Path=DataContext.CodeTables.CTPatientAllergyStatusEntityList}"
DisplayMemberPath="DisplayValue"
SelectedValuePath="PatientAllergyStatusID"
SelectedValue="{Binding Path=PatientAllergyStatusFK, Mode=TwoWay}"/>

<Button x:Name="BtnDeletePatientAllergy" Grid.Column="3"
Margin="2,2,2,5" BorderThickness="0" BorderBrush="White" HorizontalAlignment="Center"
Height="28" Width="30"
Click="BtnDeletePatientAllergy_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<Image Width="24" Height="24"
HorizontalAlignment="Center" VerticalAlignment="Center"
Source="pack://application:,,,/Medisys.SEMR.WPFClient.Controls.Resource;component/Image/DeleteIcon.png" />
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

事件:

private void BtnDeletePatientAllergy_Click(object sender, RoutedEventArgs e)
{
object obj = ((Button)sender).DataContext;

if (obj == null)
return;
else
{

PatientAllergyEntity PatientAllergyEntity = obj as PatientAllergyEntity;

if (PatientAllergyEntityList != null
&& PatientAllergyEntity.PatientAllergyID != null && PatientAllergyEntity.PatientAllergyID != Guid.Empty)
{
PatientAllergyEntityList.Remove(PatientAllergyEntity);
}

}
}

原文地址:https://www.cnblogs.com/zhaowei303/p/4794176.html