[英]Is it possible to bind the value of one textbox control to another textblock control?
[英]Popup the value in the textblock by another textbox's text
我在視圖中有一個文本框。 如果我在文本框A中輸入一個值,那么文本塊B的值將自動顯示。 這兩個屬性之間的關系在ViewModel的字典中定義。
Dictionary<string,string> dict = LoadFromDb();
// dict["01"] = "USA";
// dict["02"] = "ENG";
// If I input 01 in code textbox, then the country textblockwill display USA.
文本框和文本塊位於單元模板中。 讓我舉個例子。
<telerik:RadGridView Name="radGridView1" ShowGroupPanel="False" ShowGroupFooters="False" AutoGenerateColumns="False" ItemsSource="{Binding Persons}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Code" DataMemberBinding="{Binding Code}">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Code}" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="Country" DataMemberBinding="{Binding Country}">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Country}" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.GridViewDataColumns>
我無法將它們綁定到模型類中,因為模型類沒有字典的信息。
那么最好的方法是什么?
要將Country列綁定到整個數據條目,並使用轉換器顯示只讀值:如果它來自數據庫-未經修改,否則-從字典中獲取它。
<Window.Resources>
<local:ValueConverter x:Key="convResName"/>
</Window.Resources>
...
<telerik:GridViewDataColumn Header="Country" DataMemberBinding="{Binding}">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Converter={StaticResource convResName}, ConverterParameter={StaticResource {x:Static local:YourViewModel.yourDictAsStaticProperty}}}" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
public class ValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var person = value as Person;
if (person==null)
{
return null;
}
var dict = parameter as Dictionary<string, string>;
if (string.IsNullOrWhiteSpace(person.Country))
{
try
{
person.Country = dict[person.Code];
}
catch (KeyNotFoundException exc)
{
//handle exc
}
}
return person.Country;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.