2011-10-07 13 views
10

मैं सी # सीख रहा हूं और एक यूआई का निर्माण कर रहा हूं जो एक XML कॉन्फ़िगरेशन फ़ाइल को पूर्णांक पढ़ता है और लिखता है। यूआई विभिन्न कस्टम उपयोगकर्ता नियंत्रणों का उपयोग करता है। मेरे पास 3 रेडियूटटन उपयोगकर्ता नियंत्रण है जो एक एकल चर चर से नियंत्रित होता है (नियंत्रण 0,1,2 रिटर्न)। नियंत्रण अद्यतन ट्रिगर करने के लिए एक घटना का उपयोग करता है। यह नए int मान को निर्धारित करने के लिए 3 चेक किए गए गुणों को देखता है। लेकिन मुझे नहीं पता कि कोड के पीछे से मूल बाध्यकारी मूल्य कैसे अपडेट किया जाए। इसे एक बार बोलने के लिए हटा दिया जाता है क्योंकि दो बाइंड होते हैं .. मुख्य विंडो में और उपयोगकर्ता नियंत्रण में से एक। एक शुरुआत के रूप में इस बिंदु पर खो गया है। 3 रेडोबुटन्स में int मान पढ़ने वाले बीटीडब्ल्यू कनवर्टर का उपयोग कर काम कर रहा है।क्या मैं सी # कोड से बाध्यकारी WPF के मान को अपडेट कर सकता हूं?

यहाँ उपयोगकर्ता नियंत्रण xaml.cs है ...

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); } 
     } 
    } 
} 

यहाँ यहाँ उपयोगकर्ता नियंत्रण 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> 

है 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> 

यहां मुख्य Window.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")); 
       } 
      } 
     } 
    } 
} 

इतना कोड पोस्ट करने के लिए खेद है - मुझे लगता है कि उपयोगकर्ता शीर्ष पर xaml.cs को नियंत्रित करता है।

यहां यूआई की एक तस्वीर का एक लिंक है। मैंने उस कोड को सरल बना दिया है जिसे मैंने फिट करने के लिए पोस्ट किया है। http://www.baytower.ca/photo/uiSample.jpg

तो - 'फ़ॉन्ट रंग' (RV7sld_DFfontColor) काला (0) हरे (1) सियान हो सकता है (2)

डैनी

+0

क्या आप एक अलग रेडियो बटन की जांच करते समय बाध्यकारी 'RV7sld_DFfontColor' के मान को अपडेट करने का प्रयास कर रहे हैं? –

+0

इसके अलावा, जो भी आप पूरा करने की कोशिश कर रहे हैं, मैं आपको 'रेडियोबटन.समूहनाम' निर्भरता संपत्ति देखने के लिए आग्रह करता हूं। –

+0

हां, मैं "newValue" के साथ "RV7sld_DFfontColor" मान को अपडेट करना चाहता हूं जब एक रेडियो बटन क्लिक किया जाता है। ... इसके अलावा, मैंने पाया क्योंकि रेडियोबटन उपयोगकर्ता नियंत्रण में हैं, वे पहले से ही ठीक से समूहबद्ध हैं। जब मैंने एक समूह असाइन किया, तो उपयोगकर्ता नियंत्रण के प्रत्येक उदाहरण ने एक ही समूह का उपयोग किया ... इसलिए ऐसा लगता है कि बिना ठीक काम करता है। –

उत्तर

12

BindingOperations वर्ग आप के लिए "शक्ति" बंधन के अपडेट सक्षम बनाता है किसी भी दिशा में।

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

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

X को textBox1.Text से कॉपी करने के लिए निम्न करें::

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

मान लीजिए कि एक स्ट्रिंग संपत्ति X के पीछे कोड में वहाँ है और वहाँ XAML में एक TextBox, कि संपत्ति के लिए बाध्य है चलो

X से textBox1.Text से कॉपी करने के लिए निम्न कार्य करें:

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

धन्यवाद, मैंने आपके सुझाव से मेल खाने के लिए नया वैल्यू अनुभाग असाइन किया है। यह अभी तक काम नहीं कर रहा है- लेकिन मैं कोशिश करता रहूंगा। –

+0

यह काम करता है! चूंकि डब्ल्यूपीएफ नियंत्रण कस्टम उपयोगकर्ता नियंत्रण में होते हैं, तो दो बाइंडिंग होते हैं - एक बार मेनविंडो.एक्सएएमएल में और एक बार ui3X.xaml नियंत्रण में। एक कनवर्टर भी है जो त्रिज्या के लिए एक बूल उत्पन्न करने के लिए int मान का परीक्षण करता है।एक बार जब मैं कनवर्टर को मुख्यविंडो से उपयोगकर्ता नियंत्रण में ले गया तो यह काम करता था। मैं परिवर्तनों को दर्शाने के लिए ऊपर दिए गए कोड को अपडेट करूंगा। धन्यवाद :) –

+0

टिप के लिए धन्यवाद! यह मुझे एक बाध्यकारी अद्यतन करने के लिए केवल INotifyPropertyChanged लागू करने से बचने की अनुमति देता है। – piedar

संबंधित मुद्दे

 संबंधित मुद्दे