Puis-je mettre à jour la valeur d'une liaison WPF à partir du code C #?

Je suis en train d'apprendre le C# et la construction d'une INTERFACE utilisateur qui lit et écrit les entiers d'un fichier de configuration XML. L'INTERFACE utilisateur utilise une grande variété de commandes de l'utilisateur. J'ai un 3 radiobutton de contrôle de l'utilisateur qui se lie à une seule variable int (contrôle renvoie 0,1,2). Le contrôle utilise un événement pour déclencher la mise à jour. Il regarde la 3 isChecked propriétés pour déterminer la nouvelle valeur int. Mais je ne sais pas comment mettre à jour l'original de valeur contraignante du code derrière. C'est une fois enlevé, pour ainsi dire, car il y a deux lie..l'un dans la fenêtre principale et l'autre dans le contrôle de l'utilisateur. En tant que débutant suis perdu à ce point. BTW la lecture de la valeur int dans les 3 composants radiobutton est de travailler à l'aide d'un convertisseur.

ici est à l'utilisateur de contrôler 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); }
        }
    }
}

ici est l'utilisateur le contrôle 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>

ici est un exemple de contrôle de l'utilisateur utilisé dans 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>

ici est 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"));
                }
            }
        }
    }
}

Désolé pour l'affichage de tellement de code je pense que l'important est que l'utilisateur contrôles xaml.cs au top.

voici un lien vers une image de l'INTERFACE utilisateur.
J'ai simplifié le code que j'ai posté pour s'adapter.
http://www.baytower.ca/photo/uiSample.jpg

Donc - 'Couleur'(RV7sld_DFfontColor) peut être noir(0) vert(1) cyan(2)

Danny

source d'informationauteur Danny Maher