Can I update the value of a WPF binding from the C# code behind?

99封情书 提交于 2020-12-25 01:59:49

问题


I’m learning C# and building a UI that reads and writes integers to an XML config file. The UI uses a variety of custom user controls. I have a 3 radiobutton user control that binds to a single int variable (control returns 0,1,2). The control uses an event to trigger the update. It looks at the 3 isChecked properties to determine the new int value. But I don’t know how to update the original binding value from the code behind. It's once removed so to speak because there are two binds..one in the main window and one in the user control. As a beginner am lost at this point. BTW reading the int value into the 3 radiobuttons is working using a converter.

here is the user control 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); }
        }
    }
}

here is user control 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>

here is an example of the user control used in 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>

here is 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"));
                }
            }
        }
    }
}

Sorry for posting so much code - I think the important is the user controls xaml.cs at top.

here is a link to a picture of the UI. I've simplified the code I've posted to fit. http://www.baytower.ca/photo/uiSample.jpg

So - 'Font Color'(RV7sld_DFfontColor) can be black(0) green(1) cyan(2)

Danny


回答1:


The BindingOperations class enables you to "force" the binding updates in either direction.

Let's say there is a string property X in the code behind and there is a TextBox in XAML, bound to that property:

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

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

To copy from textBox1.Text to X do the following:

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

To copy from X to textBox1.Text do the following:

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


来源:https://stackoverflow.com/questions/7681982/can-i-update-the-value-of-a-wpf-binding-from-the-c-sharp-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!