WPF의 바인딩 프로세스를 이해하려고 시도합니다.WPF 간단한 바인딩 문제
아래 코드를 참조하십시오.
내 "viewmodel"에는 아래쪽에있는 코드를 참조하십시오. 목록보기에 항목을 채우는 관찰 가능한 컬렉션이 있습니다. 콤보 박스에서 선택된 인덱스를 설정하기 위해 symbol이라는 경로를 포함하고있는 것입니다. 이제 내 문제는 내가 listview (일부 기본값)에 추가하기 전에 다른 목록에서 콤보 상자를 채울 필요가 있다는 것입니다. WPF로 시작한 이래로 같은 클래스에서 ObservableCollections를 두 가지 사용할 수 있다고 생각했지만 작동하지 않았습니다. 그래서 어떻게 그들이 목록보기에 바인딩/추가되기 전에 datatemplate을 기본값으로 채울 수 있습니까?
이것은 내가 listview를 채우기 위해 사용하는 것으로, 하단의 viewmodelcontacts를 참조하십시오.
또한 새로운 combobox에서 사용할 수있는 observablecollection과 함께 다른 클래스를 추가하려고했지만 그 중 하나를 작동시키지 못했습니다. 채워야하는 데이터는 내 앱의 리소스로 위치한 XML 파일에서 가져옵니다.
또 다른 질문은 이미지에 명령을 추가 할 수 있습니까? 또는 button_base 클래스에서 상속 한 컨트롤에서만 사용할 수있는 명령입니까? 나는 요소 옆에있는 이미지를 사용하고 싶었고 사용자가 그 이미지를 클릭했을 때 요소를 제거했다. 내가 버튼 느낌을하지 않기 때문에 (예를 들어 마우스를 올려 놓고 클릭 할 때) * 아래의 대답에서
- 는
Window.xaml 버튼을 추가하지 않고 그것을 할 수있다 :
<r:RibbonWindow x:Class="Onyxia_KD.Windows.ContactWorkspace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="Contact card" ResizeMode="NoResize" Height="600" Width="600"
Background="White">
<r:RibbonWindow.Resources>
<DataTemplate x:Key="cardDetailFieldTemplate">
<TextBox Text="{Binding Path=Field}" MinWidth="150"></TextBox>
</DataTemplate>
<DataTemplate x:Key="cardDetailValueTemplate">
<TextBox Text="{Binding Path=Value}" MinWidth="150"></TextBox>
</DataTemplate>
<DataTemplate x:Key="cardDetailSymbolTemplate">
<!-- Here is the problem. Populating this with some default values for each entry before the selectedIndex is bound from the datasource -->
<ComboBox SelectedIndex="{Binding Path=Symbol}" DataContext="_vmSettings" ItemsSource="{Binding Symbols}" Grid.Column="1" Margin="0,0,10,0" VerticalAlignment="Center" HorizontalAlignment="Center">
<ListViewItem Padding="0,3,0,3" Content="{Binding Path=Value}" />
</ComboBox>
</DataTemplate>
<DataTemplate x:Key="cardDetailCategoryTemplate">
<ComboBox SelectedIndex="{Binding Path=Category}" Grid.Column="1" Margin="0,0,10,0" VerticalAlignment="Center" HorizontalAlignment="Center">
<!--same as the combobox above but categories instead of symbols-->
</ComboBox>
</DataTemplate>
</r:RibbonWindow.Resources>
...
<ListView ItemsSource="{Binding ContactData}" Foreground="Black" SelectionMode="Single" x:Name="cardDetailList" KeyDown="cardDetailList_KeyDown">
<ListView.View>
<GridView>
<GridViewColumn Header="Category" Width="auto" CellTemplate="{StaticResource cardDetailCategoryTemplate}"></GridViewColumn>
<GridViewColumn Header="Field" Width="auto" CellTemplate="{StaticResource cardDetailFieldTemplate}"></GridViewColumn>
<GridViewColumn Header="Symbol" Width="70" CellTemplate="{StaticResource cardDetailSymbolTemplate}"></GridViewColumn>
<GridViewColumn Header="Value" Width="auto" CellTemplate="{StaticResource cardDetailValueTemplate}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
뒤에
코드 :
private ViewModelContacts _vm;
public ContactWorkspace()
{
InitializeComponent();
_vm = new ViewModelContacts();
this.DataContext = _vm;
}
private void Run_MouseUp(object sender, MouseButtonEventArgs e)
{
_vm.AddNewDetail();
}
private void Image_MouseUp(object sender, MouseButtonEventArgs e)
{
_vm.AddNewDetail();
}
private void cardDetailList_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (MessageBox.Show("Are you sure that you want to delete this?", "Warning!", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
_vm.RemoveDetail(cardDetailList.SelectedIndex);
}
}
}
ViewModelContacts :
public ObservableCollection<ContactCardData> ContactData { get; set; }
public ViewModelContacts()
{
ContactData = new ObservableCollection<ContactCardData>();
Populate();
}
private void Populate()
{
ContactData.Add(new ContactCardData("Test", 0, 0, "Value123"));
ContactData.Add(new ContactCardData("Test2", 1, 1, "Value1234"));
ContactData.Add(new ContactCardData("Test3", 2, 2, "Value1235"));
ContactData.Add(new ContactCardData("Test4", 3, 3, "Value12356"));
}
public void UpdateNode()
{
ContactData.ElementAt(0).Value = "Giraff";
}
public void AddNewDetail()
{
ContactData.Add(new ContactCardData());
}
public void RemoveDetail(int position)
{
ContactData.RemoveAt(position);
}
ViewModelContactData : 마지막 질문에 대한
public class ContactCardData : DependencyObject
{
public int Category
{
get { return (int)GetValue(CategoryProperty); }
set { SetValue(CategoryProperty, value); }
}
// Using a DependencyProperty as the backing store for Category. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CategoryProperty =
DependencyProperty.Register("Category", typeof(int), typeof(ContactCardData), new UIPropertyMetadata(0));
public string Field
{
get { return (string)GetValue(FieldProperty); }
set { SetValue(FieldProperty, value); }
}
// Using a DependencyProperty as the backing store for Field. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FieldProperty =
DependencyProperty.Register("Field", typeof(string), typeof(ContactCardData), new UIPropertyMetadata(""));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(ContactCardData), new UIPropertyMetadata(""));
public int Symbol
{
get { return (int)GetValue(SymbolProperty); }
set { SetValue(SymbolProperty, value); }
}
// Using a DependencyProperty as the backing store for Symbol. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SymbolProperty =
DependencyProperty.Register("Symbol", typeof(int), typeof(ContactCardData), new UIPropertyMetadata(0));
public ContactCardData()
{
}
public ContactCardData(string field, int category, int symbol, string value)
{
this.Symbol = symbol;
this.Category = category;
this.Field = field;
this.Value = value;
}
}
안녕하세요, 응답 해 주셔서 감사합니다. 나는 나 자신을 100 % 설명하지 않았다. listview 및 내 코드에 대한 XAML을 추가했는데, 그 중 일부는 작동하지 않는 심볼이었습니다. 나는 무엇을 묶을 지 전혀 모른다. 모든 미리 정의 된 XML 콘텐츠를 외부 클래스에 추가하고 모든 미리 정의 된 데이터 바인딩에 사용하고 싶습니다. 하지만 어떻게 동시에 두 datacontexts에 콤보 상자를 바인딩 할 수 있습니까? 하나는 현재 선택된 인덱스를 적용하고 다른 하나는 디폴트 값으로 표시하기 전에 콤보 박스를 채우기위한 것입니다. – Patrick