我可以从后面的C#代码更新WPF绑定的值吗?

问题描述:

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

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在顶部。

这里是一个链接到UI的图片。 我简化了我发布的适合的代码。 http://www.baytower.ca/photo/uiSample.jpg

所以 - '字体颜色'(RV7sld_DFfontColor)可以是黑色(0)绿色(1)青(2)

丹尼

+0

您是否尝试更新绑定'RV7sld_DFfontColor'的值时检查不同的单选按钮? –

+0

此外,为了实现您的目标,我强烈建议您查看RadioButton.GroupName依赖项属性。 –

+0

是的,我想在单击radioButton时用“newValue”更新“RV7sld_DFfontColor”值。 ...另外,我发现因为radioButtons在用户控件中,所以它们已经被正确分组了。当我分配一个组时,用户控件的每个实例都使用相同的组......所以它似乎没有工作就可以。 –

BindingOperations类可以 “逼” 的绑定更新在任何方向。

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

// 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做到以下几点:

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

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

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

谢谢,我已经更改了分配newValue部分以符合您的建议。它还没有工作 - 但我会继续尝试。 –

+0

它的工作原理!由于WPF控件位于自定义用户控件中,因此有两个绑定 - 一次位于MainWindow.xaml中,一次位于ui3X.xaml控件中。还有一个转换器测试int值,为单选按钮生成一个bool。一旦我将转换器从mainWindow移动到用户控件的工作。我将更新上面的代码以反映更改。谢谢:) –

+0

感谢您的提示!它允许我避免实现INotifyPropertyChanged来更新单个绑定。 – piedar