2011-10-07 25 views
10

Tôi đang học C# và xây dựng một giao diện người dùng đọc và ghi số nguyên vào tệp cấu hình XML. Giao diện người dùng sử dụng nhiều loại điều khiển người dùng tùy chỉnh khác nhau. Tôi có 3 điều khiển người dùng radiobutton liên kết với một biến int đơn (điều khiển trả về 0,1,2). Điều khiển sử dụng sự kiện để kích hoạt cập nhật. Nó xem xét 3 thuộc tính isChecked để xác định giá trị int mới. Nhưng tôi không biết cách cập nhật giá trị ràng buộc ban đầu từ mã phía sau. Nó đã từng được loại bỏ để nói bởi vì có hai liên kết .. một trong cửa sổ chính và một trong điều khiển người dùng. Là một người mới bắt đầu bị mất vào thời điểm này. BTW đọc giá trị int vào 3 radiobutton đang làm việc bằng cách sử dụng một công cụ chuyển đổi.Tôi có thể cập nhật giá trị của một ràng buộc WPF từ mã C# phía sau không?

đây là xaml.cs điều khiển người dùng ...

namespace btsRV7config.controls 
{ 
    public partial class ui3X : UserControl 
    { 
     public ui3X() 
     { 
      InitializeComponent(); 
     } 

     void _event(object sender, RoutedEventArgs e) 
     { 
      int newValue = 0; 
      if (rdbttn1.IsChecked == true) { newValue = 0; } 
      else if (rdbttn2.IsChecked == true) { newValue = 1; } 
      else if (rdbttn3.IsChecked == true) { newValue = 2; } 
      txtb.Text = newValue.ToString(); //tempRemove 

      // !!! assign newValue to Binding Source !!! 
      //--------------------------------------------- 
      uiBinding1 = newValue; 
      BindingOperations.GetBindingExpression(rdbttn1, RadioButton.IsCheckedProperty).UpdateSource(); 
      //--------------------------------------------- 
     } 

     public static readonly DependencyProperty uiBinding1Property = DependencyProperty.Register("uiBinding1", typeof(int), typeof(ui3X)); 
     public int uiBinding1 
     { 
      get { return (int)GetValue(uiBinding1Property); } 
      set { SetValue(uiBinding1Property, value); } 
     } 

     public static readonly DependencyProperty uiBinding2Property = DependencyProperty.Register("uiBinding2", typeof(int), typeof(ui3X)); 
     public int uiBinding2 
     { 
      get { return (int)GetValue(uiBinding2Property); } 
      set { SetValue(uiBinding2Property, value); } 
     } 

     public static readonly DependencyProperty uiBinding3Property = DependencyProperty.Register("uiBinding3", typeof(int), typeof(ui3X)); 
     public int uiBinding3 
     { 
      get { return (int)GetValue(uiBinding3Property); } 
      set { SetValue(uiBinding3Property, value); } 
     } 
    } 
} 

đây là điều khiển người dùng XAML

<UserControl x:Class="btsRV7config.controls.ui3X" 
      x:Name="root" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel Orientation="Horizontal" Height="22"> 

     <RadioButton Name="rdbttn1" VerticalAlignment="Center" Margin="0 0 10 0" 
        IsChecked="{Binding ElementName=root, Path=uiBinding1}" 
        Click="_event" /> 

     <RadioButton Name="rdbttn2" VerticalAlignment="Center" Margin="0 0 10 0" 
        IsChecked="{Binding ElementName=root, Path=uiBinding2}" 
        Click="_event" /> 

     <RadioButton Name="rdbttn3" VerticalAlignment="Center" 
        IsChecked="{Binding ElementName=root, Path=uiBinding3}" 
        Click="_event" /> 

     <TextBox Name="txtb" Margin="5 0 0 0" Width="20" Height="17" /> <!-- tempRemove --> 
    </StackPanel> 
</UserControl> 

đây là một ví dụ về điều khiển người dùng sử dụng trong MainWindow.xaml

<Window x:Class="btsRV7config.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:controls="clr-namespace:btsRV7config.controls" 
     xmlns:converter="clr-namespace:btsRV7config.converters" 
     Title="Vans RV7 Configuration" Height="350" Width="525" > 
    <Window.Resources> 
     <converter:Int_Int_Bool_Converter x:Key="Int_Int_Bool" /> 
    </Window.Resources> 

    <Grid> 
     <controls:ui3X uiName="Font Color" ui1="Black" ui2="Green" ui3="Cyan" 
         uiBinding1="{Binding RV7sld_DFfontColor, Converter={StaticResource Int_Int_Bool}, ConverterParameter=0}" 
         uiBinding2="{Binding RV7sld_DFfontColor, Converter={StaticResource Int_Int_Bool}, ConverterParameter=1}" 
         uiBinding3="{Binding RV7sld_DFfontColor, Converter={StaticResource Int_Int_Bool}, ConverterParameter=2}" /> 
    </Grid> 
</Window> 

đây là MainWindow.xaml.cs

namespace btsRV7config 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      record data = new record(); 
      DataContext = data; 
     } 
    } 

    public class record : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private int _RV7sld_DFfontColor = RV7sld_dict["DFfontColor"]; 
     public int RV7sld_DFfontColor 
     { 
      get 
      { return _RV7sld_DFfontColor; } 
      set 
      { 
       _RV7sld_DFfontColor = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("RV7sld_DFfontColor")); 
       } 
      } 
     } 
    } 
} 

Xin lỗi vì đã đăng rất nhiều mã - tôi nghĩ điều quan trọng là người dùng điều khiển xaml.cs ở trên cùng.

đây là liên kết đến hình ảnh giao diện người dùng. Tôi đã đơn giản hóa mã tôi đã đăng để vừa. http://www.baytower.ca/photo/uiSample.jpg

Vì vậy, - 'Màu chữ' (RV7sld_DFfontColor) có thể có màu đen (0) màu xanh lá cây (1) màu lục lam (2)

Danny

+0

Bạn đang cố gắng cập nhật giá trị Binding 'RV7sld_DFfontColor' khi chọn một nút radio khác? –

+0

Ngoài ra, đối với những gì bạn đang cố gắng hoàn thành, tôi khuyên bạn nên xem xét thuộc tính phụ thuộc 'RadioButton.GroupName'. –

+0

Có, tôi muốn cập nhật giá trị "RV7sld_DFfontColor" với "newValue" khi nút radioButton được nhấp. ... Ngoài ra, tôi tìm thấy bởi vì các radioButtons là trong một điều khiển người dùng họ đã được nhóm đúng cách. Khi tôi chỉ định một nhóm, mỗi trường hợp của điều khiển người dùng được sử dụng cùng một nhóm ... vì vậy nó có vẻ làm việc ok mà không có. –

Trả lời

12

Lớp BindingOperations cho phép bạn "lực" các bản cập nhật ràng buộc theo một trong hai hướng.

Hãy nói rằng có một chuỗi bất động sản X trong mã đằng sau và có một TextBox trong XAML, ràng buộc với tài sản đó:

// C#: 
public string X { get; set; } 

// XAML: 
<TextBox Name="textBox1" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}, Path=X}" /> 

Để sao chép từ textBox1.Text để X làm như sau:

BindingOperations.GetBindingExpression(textBox1, TextBox.TextProperty).UpdateSource(); 

Để sao chép từ X đến textBox1.Text, hãy làm như sau:

BindingOperations.GetBindingExpression(textBox1, TextBox.TextProperty).UpdateTarget(); 
+0

Cảm ơn, tôi đã thay đổi phần Chỉ định giá trị mới để phù hợp với đề xuất của bạn. Nó vẫn chưa hoạt động- nhưng tôi sẽ tiếp tục cố gắng. –

+0

Nó hoạt động! Bởi vì các điều khiển WPF trong một điều khiển người dùng tùy chỉnh có hai ràng buộc - một lần trong MainWindow.xaml và một lần trong điều khiển ui3X.xaml. Ngoài ra còn có một bộ chuyển đổi kiểm tra giá trị int để tạo ra một bool cho radiobutton.Một khi tôi chuyển trình chuyển đổi từ mainWindow sang điều khiển người dùng nó đã hoạt động. Tôi sẽ cập nhật mã ở trên để phản ánh các thay đổi. Cảm ơn :) –

+0

Cảm ơn bạn đã tip! Nó cho phép tôi tránh thực hiện INotifyPropertyChanged chỉ để cập nhật một ràng buộc duy nhất. – piedar

Các vấn đề liên quan