我可以从后面的 C# 代码更新 WPF 绑定的值吗? [英] Can I update the value of a WPF binding from the C# code behind?

查看:24
本文介绍了我可以从后面的 C# 代码更新 WPF 绑定的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C# 并构建一个 UI 来读取和写入整数到 XML 配置文件.UI 使用各种自定义用户控件.我有一个绑定到单个 int 变量的 3 个单选按钮用户控件(控件返回 0,1,2).该控件使用一个事件来触发更新.它查看 3 个 isChecked 属性以确定新的 int 值.但是我不知道如何从后面的代码中更新原始绑定值.可以这么说,它曾经被删除过,因为有两个绑定……一个在主窗口中,一个在用户控件中.作为初学者,我迷失在这一点上.顺便说一句,正在使用转换器将 int 值读入 3 个单选按钮.

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.

这里是用户控件 xaml.cs...

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

这里是用户控件 xaml

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>

这是 MainWindow.xaml 中使用的用户控件示例

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>

这里是 MainWindow.xaml.cs

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

抱歉发布了这么多代码 - 我认为重要的是用户控制顶部的 xaml.cs.

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

这是 UI 图片的链接.我已经简化了我发布的代码以适应.http://www.baytower.ca/photo/uiSample.jpg

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

所以 - 'Font Color'(RV7sld_DFfontColor) 可以是 black(0) green(1) cyan(2)

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

丹尼

推荐答案

BindingOperations 类使您能够在任一方向强制"绑定更新.

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

假设在后面的代码中有一个字符串属性 X 并且在 XAML 中有一个 TextBox 绑定到该属性:

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}" />

要从 textBox1.Text 复制到 X,请执行以下操作:

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

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

要从 X 复制到 textBox1.Text,请执行以下操作:

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

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

这篇关于我可以从后面的 C# 代码更新 WPF 绑定的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆