Bağlamak için bağımlılık özelliklerine sahip bir kullanıcı denetimi oluşturmaya çalışıyorum. Dahili olarak, aynı özelliklere bağlı olan bir ComboBox
var, ancak bağlanma yalnızca bir şekilde çalışır. ComboBox
, ItemsSource
'dan doldurur, ancak SelectedItem
, bağlandığım görünüm modeline geri dönmez.WPF Kullanıcı Denetimi'nin bağımlılık özelliklerimi görünüm modelimi nasıl günceller?
basitleştirilmiş bir örnek: koduyla
<UserControl x:Class="PeopleControlTest.PeopleControl"
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"
mc:Ignorable="d" d:DesignHeight="56" d:DesignWidth="637">
<StackPanel >
<ComboBox Margin="11"
ItemsSource="{Binding BoundPeople, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding BoundSelectedPerson, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</StackPanel>
: Bu kullanıcı kontrolü
public class PeopleViewModel : INotifyPropertyChanged
{
public PeopleViewModel()
{
People = new List<string>(new [] {"John", "Alfred","Dave"});
SelectedPerson = People.FirstOrDefault();
}
public event PropertyChangedEventHandler PropertyChanged;
private IEnumerable<string> _people;
public IEnumerable<string> People
{
get { return _people; }
set
{
_people = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("People"));
}
}
}
private string _selectedPerson;
public string SelectedPerson
{
get { return _selectedPerson; }
set
{
_selectedPerson = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("SelectedPerson"));
}
}
}
}
:
Bu kullanıcı kontrolü ile bağlamak için görünüşüdür modelidir arkasında
public partial class PeopleControl : UserControl
{
public PeopleControl()
{
InitializeComponent();
}
public static readonly DependencyProperty BoundPeopleProperty =
DependencyProperty.Register("BoundPeople", typeof(IEnumerable<string>), typeof(PeopleControl), new UIPropertyMetadata(null));
public static readonly DependencyProperty BoundSelectedPersonProperty =
DependencyProperty.Register("BoundSelectedPerson", typeof(string), typeof(PeopleControl), new UIPropertyMetadata(""));
public IEnumerable<string> BoundPeople
{
get { return (IEnumerable<string>)GetValue(BoundPeopleProperty); }
set { SetValue(BoundPeopleProperty, value); }
}
public string BoundSelectedPerson
{
get { return (string)GetValue(BoundSelectedPersonProperty); }
set { SetValue(BoundSelectedPersonProperty, value); }
}
}
Ve
<Window x:Class="PeopleControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:PeopleControlTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<controls:PeopleControl
BoundPeople="{Binding People}"
BoundSelectedPerson="{Binding SelectedPerson}"/>
</Grid>
</Window>
kullanıcı kontrolünde combobox adları ile doldurur (ViewModel örneğine ayarlı pencereler veri bağlamıyla) ana pencerede kullanıcı kontrolü bağlamak nasıl bu olduğunu ancak farklı bir ad seçtiğimde bu, görünüm modeline geri güncellenmez. Burada ne eksik olduğum hakkında bir fikrin var mı?
Teşekkürler!
Büyük, teşekkür ederim çalıştığını !!! Bazı nedenlerden dolayı tüm DP'lerin varsayılan olarak iki şekilde çalıştığını okudum. O seçeneği bulamadığım UIPropertyMetadata kullanıyordum. – Stefan
_ (Bu çözüm gayet iyi. Sadece aynı sorunu yaşayabilen ve bir çözüm ararken yollarını bulan insanlar için bir not eklemek istiyorum.) _ Bir bağımlılık özelliğine bağlandığınız özellik UserControl bir set() yöntemine sahip olmalıdır. – skst