我有一个带文本框的窗口。填写此表单有两个模板(两组数据)。用户单击单选按钮并选择模板。之后,文本框由模板中的数据填充。
<Window x:Class="WpfTestApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="Height" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<RadioButton GroupName="Templates" Content="Template1" IsChecked="True"></RadioButton>
<RadioButton GroupName="Templates" Content="Template2"></RadioButton>
</StackPanel>
<Label Grid.Row="1" Grid.Column="0">Field1</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="TbField1"></TextBox>
<Label Grid.Row="2" Grid.Column="0">Field2</Label>
<TextBox Grid.Row="2" Grid.Column="1" Name="TbField2"></TextBox>
<Label Grid.Row="3" Grid.Column="0">Field3</Label>
<TextBox Grid.Row="3" Grid.Column="1" Name="TbField3"></TextBox>
<Button Grid.Row="4" Grid.ColumnSpan="2" Name="BtnGetData" Content="GetData" Click="BtnGetData_Click"></Button>
</Grid>
public partial class MainWindow : Window
{
private static DataSetTest data1;
private static DataSetTest data2;
private static DataBingings bindings;
public MainWindow()
{
InitializeComponent();
data1 = new DataSetTest("field1_1", "field2_1", "field3_1");
data2 = new DataSetTest("field1_2", "field2_2", "field3_2");
bindings = new DataBingings();
bindings.Add(TbField1, data1, "Field1", data2, "Field1");
bindings.Add(TbField2, data1, "Field2", data2, "Field2");
bindings.Add(TbField3, data1, "Field3", data2, "Field3");
bindings.SetData();
}
private void BtnGetData_Click(object sender, RoutedEventArgs e)
{
bindings.GetData();
MessageBox.Show(data1.Field1);
}
}
public class DataSetTest
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public DataSetTest()
{
}
public DataSetTest(string field1, string field2, string field3)
{
Field1 = field1;
Field2 = field2;
Field3 = field3;
}
}
public class DataBingings
{
List<BindingExpressionBase> bindings = new List<BindingExpressionBase>();
private Binding AddBinding(string propertyName, object data)
{
Binding b = new Binding(propertyName);
b.Mode = BindingMode.TwoWay;
b.Source = data;
b.ConverterCulture = CultureInfo.CurrentCulture;
b.NotifyOnSourceUpdated = false;
b.NotifyOnTargetUpdated = false;
return b;
}
private MultiBinding AddMultiBinding(string propertyName1, object data1, object data2, string propertyName2)
{
MultiBinding mb = new MultiBinding();
mb.Converter = new CustomMultiValueConvertor();
mb.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
Binding b = AddBinding(propertyName1, data1);
mb.Bindings.Add(b);
b = AddBinding(propertyName2, data2);
mb.Bindings.Add(b);
return mb;
}
public void Add(TextBox control, object data1, string propertyName1, object data2, string propertyName2)
{
MultiBinding b = AddMultiBinding(propertyName1, data1, data2, propertyName2);
BindingExpressionBase be = BindingOperations.SetBinding(control, TextBox.TextProperty, b);
bindings.Add(be);
}
public void SetData()
{
foreach (BindingExpressionBase b in bindings)
{
b.UpdateTarget();
}
}
public void GetData()
{
foreach (BindingExpressionBase b in bindings)
{
b.UpdateSource();
}
}
}
public class CustomMultiValueConvertor : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0];
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return value as object[];
}
}
类DataBindings包含添加多重绑定和将数据插入文本框或从中获取数据的方法。 但是,如何将参数从RadioButton发送到多重绑定以选择第一个或第二个绑定?
答案 0 :(得分:0)
如果没有a good, minimal, complete code exampl,就无法知道什么是最适合您的方案的设计。您在这里没有提供太多上下文,这必然会限制我们对您的代码的了解。那说......
您可以设置MultiBinding.ConverterParameter
属性;这将设置作为parameter
和Convert()
方法的ConvertBack()
参数传递的值。
但请注意,你不能将它绑定到任何东西;它只能设置为某个特定值。因此,您必须根据您的无线电组的状态明确更新它。鉴于此,不考虑转换器或MultiBinding
本身可能更有意义,只需根据单选按钮选择更新绑定本身。
也就是说,当单选按钮选择发生变化时,只需根据选择将 <{em> 当然,可能有一种更好的,XAML声明的方式来做你想做的事情。同样,如果没有一个好的代码示例,那么推测这个特定点是不切实际的。如果上述内容看起来不像您真正想做的那样,请考虑改进问题,提供更好的代码示例,明确说明该代码示例的作用,以及它与您希望它做的不同。 / p>
propertyName1
或propertyName2
绑定到所需的Text
属性。< / p>