2011-01-18 26 views
5

Kodumun, aynı sorunu yaşayan basitleştirilmiş bir sürümünü oluşturdum. Sorun şu ki, özel kontrolümdeki bağımlılık özelliği neden modelde değiştiğinde güncellenmiyor.Özel Denetime Bağımlılık Özellik Bağlama ve Güncelleştirme

Modeli:

public class MainWindowModel : INotifyPropertyChanged 
{ 
    private bool isChecked; 
    public bool IsChecked { get { return isChecked; } set { isChecked = value; OnPropertyChanged("IsChecked"); } } 

    public event PropertyChangedEventHandler PropertyChanged; 
    void OnPropertyChanged(string prop) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
    } 
} 

XAML:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:custom="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <custom:CustomTextbox x:Name="TextboxName" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" TextChanged="CustomTextbox_TextChanged"> 
     <custom:CustomTextbox.CustomTextboxItems> 
      <custom:CustomTextboxItem IsChecked="{Binding IsChecked}" /> 
     </custom:CustomTextbox.CustomTextboxItems> 
    </custom:CustomTextbox> 

    <Button Content="Do It" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20" /> 
</Grid> 
</Window> 

Kod Arkası:

public partial class MainWindow : Window 
{ 
    MainWindowModel model; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     model = new MainWindowModel(); 
     this.DataContext = model; 
    } 

    private void CustomTextbox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     model.IsChecked = true; 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     if (TextboxName.CustomTextboxItems[0].IsChecked) 
     { 
      TextboxName.Text = "Property successfully changed"; 
     } 
    } 
} 

Özel Kontrol:

public class CustomTextbox : TextBox 
{ 
    public CustomTextbox() 
    { 
     CustomTextboxItems = new ObservableCollection<CustomTextboxItem>(); 
    } 

    public ObservableCollection<CustomTextboxItem> CustomTextboxItems { get; set; } 
} 

public class CustomTextboxItem : FrameworkElement 
{ 
    public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CustomTextboxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 

    public bool IsChecked 
    { 
     get { return (bool)GetValue(IsCheckedProperty); } 

     set { SetValue(IsCheckedProperty, value); } 
    } 
} 

Özel denetimde görebileceğiniz gibi, bağlamak istediğim bağımlılık özelliklerine sahip nesneler içeren öğelerden oluşan bir koleksiyona sahibim. Bu yüzden, xaml'deki nesneleri yaratıyorum ve bağlamayı kuruyorum, ancak modeldeki bağlanmış özelliği güncelleştirdiğimde, özel denetimde onu değiştirmiyor. Herhangi bir fikir?

cevap

5

Visual Studio çıktı penceresinde bağlama hataları olup olmadığına bakın. Onay kutusundaki bağlamanın başarısız olduğunu belirten bir şey bulacaksınız.

CustomTextBox ürününüzün üzerinde bir bağlayıcı bağladığınız CustomTextBoxItem nesnelerin bir koleksiyonu vardır. Bununla birlikte, hiçbir noktayı mantıksal bir ağaca eklemiyorsunuz. Bu öğeleri mantıksal ağaca nasıl ekleyeceğinizi görmek için yayınımı here okuyun.

+0

Çok teşekkür ederim! – gamzu07

+0

Bu bağlantı öldü, lütfen güncelleyin @kentboogaart –

İlgili konular