저는 WPF에 익숙하지 않아 객체를 생성하기 위해 ObjectDataProvider를 사용하려고합니다. 특히 텍스트를 텍스트 상자에 입력 할 때 개체를 업데이트하고 싶습니다.ObjectDataProvider를 사용하여 객체 인스턴스화
이public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
}
}
}
public class MyStrings : List<String>
{
public MyStrings()
{
this.Add("Test1");
this.Add("Test2");
this.Add("Test3");
this.Add("Test4");
}
}
내 질문에 내가 ObjectDataProvider에 의해 생성 된 객체를 참조하는 데 사용합니까 무엇 때문에 나는를 조작 할 수있다 :
<Window x:Class="bindings.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:bindings"
Title="Bindings" Height="410" Width="1044">
<Window.Resources>
<ObjectDataProvider x:Key ="MyStringData" ObjectType="{x:Type local:MyStrings}" />
</Window.Resources>
<StackPanel>
<TextBox Height="23" Name="textBox1" Width="120" KeyDown="textBox1_KeyDown" />
<ListBox Name="theListBox" Width="200" Height="79"
ItemsSource ="{Binding Source={StaticResource MyStringData}}"/>
</StackPanel>
다음
뒤에 코드입니다 : 여기 XAML입니다 MyString이 작성한 인스턴스 감사.
새로운 XAML :
<Window x:Class="bindings.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:bindings"
Title="Bindings" Height="410" Width="1044">
<Window.Resources>
<ObjectDataProvider x:Key ="MyStringData" ObjectType="{x:Type local:MyStrings}" x:Name="myProvider" />
</Window.Resources>
<StackPanel>
<TextBox Height="23" Name="textBox1" Width="120" KeyDown="textBox1_KeyDown" />
<ListBox Name="theListBox" Width="200" Height="79"
ItemsSource ="{Binding Source={StaticResource MyStringData}}"/>
</StackPanel>
새로운 코드 뒤에 :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
MyStrings temp = myProvider.ObjectInstance;
}
}
}
public class MyStrings : List<String>
{
public MyStrings()
{
this.Add("Test1");
this.Add("Test2");
this.Add("Test3");
this.Add("Test4");
}
}
VS는 "이 범위에서 중복 이름 'myProvider'를 등록 할 수 없다는 것을 알려줍니다. 왜? – BobD
@BobD 변경 내용을 반영하도록 코드를 업데이트 할 수 있습니까 (XAML뿐 아니라 XAML에서 이름을 지정한 다음 코드 숨김에서도 선언 한 것처럼 들립니다. – CodingGorilla
@Codeing Gorilla - 편집을 참조하십시오. - 고마워요. – BobD