내 ListBox의 SelectedItem에 문제가 있습니다. 내 HealthDiseaseViewModel의 Highlighted 속성에 선택한 항목을 넣고 싶습니다. 여기 내 ViewModel이 있습니다.
public class HealthDiseaseViewModel : ObservableCollection<HealthDisease>
{
private string _feedBack;
HealthDiseaseDb _db = new HealthDiseaseDb();
public HealthDiseaseRetrieveSingleCommand RetrieveSingleCommand { get; set; }
public HealthDiseaseRetrieveManyCommand RetrieveManyCommand { get; set; }
public HealthDiseaseUpdateCommand UpdateCommand { get; set; }
public HealthDisease Entity { get; set; }
public HealthDisease Highlighted { get; set; }
public List<HealthDisease> EntityList { get; set; }
public HealthDiseaseViewModel()
{
RetrieveSingleCommand = new HealthDiseaseRetrieveSingleCommand(this);
RetrieveManyCommand = new HealthDiseaseRetrieveManyCommand(this);
UpdateCommand = new HealthDiseaseUpdateCommand(this);
Highlighted = new HealthDisease();
Entity = new HealthDisease();
RetrieveMany(Entity);
}
#region Methods
public void Retrieve(HealthDisease parameters)
{
Highlighted = _db.Retrieve(parameters);
}
public void RetrieveMany(HealthDisease parameters)
{
EntityList = new List<HealthDisease>();
EntityList = _db.RetrieveMany(parameters);
IList<HealthDisease> toBeRemoved = Items.ToList();
foreach (var item in toBeRemoved)
{
Remove(item);
}
foreach (var item in EntityList)
{
Add(item);
}
}
public void Insert(HealthDisease entity)
{
bool doesExist = false;
if (_db.Insert(entity, SessionHelper.CurrentUser.Id, ref doesExist))
{
_feedBack = "Item Successfully Saved!";
RetrieveMany(new HealthDisease());
}
else if (doesExist)
{
_feedBack = "Item Already Exists!";
}
else
{
_feedBack = "Not All Fields Were Filled-In!";
}
MessageBox.Show(_feedBack, "Item Insertion");
}
public void Update(HealthDisease entity)
{
bool doesExist = false;
if (_db.Update(entity, SessionHelper.CurrentUser.Id, ref doesExist))
{
_feedBack = "Item Successfully Updated!";
RetrieveMany(new HealthDisease());
}
else if (doesExist)
{
_feedBack = "Item Already Exists!";
}
else
{
_feedBack = "Not All Fields Were Filled-In!";
}
MessageBox.Show(_feedBack, "Item Edition");
}
public void Delete(HealthDisease entity)
{
var answer = MessageBox.Show(String.Format("Are you sure you want to delete \n{0}?", entity.Name),
"Item Deletion", MessageBoxButtons.YesNo);
if (answer == DialogResult.No)
{
return;
}
if (_db.Delete(entity, SessionHelper.CurrentUser.Id))
{
_feedBack = "Item Successfully Deleted!";
RetrieveMany(new HealthDisease());
}
MessageBox.Show(_feedBack, "Item Deletion");
}
#endregion
}
내 ListBox의 SelectedItem을 Highlighted에 바인딩하고 Highlighted.Name 및 Highlighted.Description을 TextBlocks에 원했지만 TextBlocks에 SelectedItem이 표시되지 않습니다. SelectedItem.Name 및 SelectedItem.Description을 사용하여이 문제를 해결할 수 있지만 문제는 아직 저장 버튼을 누르지 않아도 자동으로 ListBox를 업데이트한다는 것입니다. 강조 표시된 개체를 사용하면이 문제가 해결되지만 지금은 몇 시간 동안 좌절감을 느끼고 있습니다. 여기 내 마크 업이 있습니다. 내 ViewModel에서 UpdateCommand에 바인딩 된 SaveButton을 생략했습니다.
<Grid Name="MainGrid" Background="Aqua" MinWidth="500" MinHeight="400"
DataContext="{Binding Source={StaticResource HealthViewModel}}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Highlighted.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
<ListBox x:Name="EntityListBox" Margin="10,0" Height="380"
ItemsSource="{Binding }"
DisplayMemberPath="Name"
SelectedItem="{Binding Path=Highlighted, Mode=TwoWay,
Converter={StaticResource ToHealthDiseaseConverter},
UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Grid>
빠른 답변을 드릴 수 있지만 여기서 멈추지 않고 객체의 속성에 바인딩이 작동하지 않는 이유를 찾으려고 노력하지 않기를 바랍니다. DependencyProperty는 적절한 발생 PropertyChanged 트리거를 사용하여 INotifyPropertyChanged를 구현하는 인스턴스에 바인딩합니다. 인스턴스의 "값"에 바인딩되지 않습니다. Highlighted.Name 바인딩이 작동하지 않는 이유를 파악할 수 있는지 확인하십시오.
당신을 위해 간단한 샘플을 만들었습니다
<Window x:Class="WpfTestProj.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTestProj"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MainViewModel, IsDesignTimeCreatable=False}"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel>
<TextBlock Text="{Binding Path=HighlightedName}" />
</StackPanel>
<StackPanel>
<ListBox x:Name="EntityListBox" Margin="10,0"
ItemsSource="{Binding EntityList}"
DisplayMemberPath="Name"
SelectedItem="{Binding Path=Highlighted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</StackPanel>
</Grid>
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class HealthDisease
{
public string Name { get; set; }
}
public class MainViewModel : ViewModelBase
{
public ObservableCollection<HealthDisease> EntityList { get; set; }
public MainViewModel()
{
RetrieveMany();
}
private void RetrieveMany()
{
EntityList = new ObservableCollection<HealthDisease>
{
new HealthDisease {Name = "Disease A"},
new HealthDisease {Name = "Disease B"},
new HealthDisease {Name = "Disease C"}
};
}
private HealthDisease highlighted;
public HealthDisease Highlighted
{
get { return highlighted; }
set
{
highlighted = value;
OnPropertyChanged();
OnPropertyChanged("HighlightedName");
}
}
public string HighlightedName
{
get { return Highlighted == null ? string.Empty : Highlighted.Name; }
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다