В моем пользовательском контроле я использовал один комбинированный список и текстовые поля, которые я буду использовать этот пользовательский контроль на 4 вкладках (Просмотр, Добавить, Обновить, Удалить). На вкладке просмотра я использовал сетку данных для привязки данных к текстовым полям. Я написал Viemodel (VMvisibility) для видимости кнопки со списком, сохранения, удаления, обновления. И еще одна viewmodel (ViewModelCust) для привязки данных к сетке данных и к текстовым полям.
Проблема, с которой я сталкиваюсь, заключается в том, что при наличии данных модели видимости VMvisibility не привязывается к текстовым полям из сетки данных, но когда я удаляю модель представления VMvisibility и использую это свойство visibilty внутри того же класса окна, я могу связать данные с текстовыми полями , но он привязывает данные к текстовым полям ко всем 4 вкладкам вместо одной вкладки
Код UserControl.xaml
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Top" >
<StackPanel Orientation="Horizontal" Name="CustIDList" Visibility="{Binding CustIDVisibility }" >
<Button x:Name="CustID" Content="Customer ID" Width="100" Margin="82 0 0 0" />
<ComboBox Height="25" Width="180" x:Name="ComboID" ItemsSource="{Binding CustomersData}"
DisplayMemberPath="CustomerID" SelectedItem="{Binding GetCustomerData}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label x:Name="CustName" Content="Customer Name" Width="100" Margin="82 0 0 0"/>
<TextBox x:Name="BoxName" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerName}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label x:Name="CustAge" Content=" Age" Width="100" Margin="82 0 0 0"/>
<TextBox x:Name="BoxAge" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerAge}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label x:Name="CustAddress" Content="Address" Width="100" Margin="82 0 0 0"/>
<TextBox x:Name="BoxAddress" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerAddress}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label x:Name="CustEmail" Content="Email ID" Width="100" Margin="82 0 0 0"/>
<TextBox x:Name="BoxEmail" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerEmailID}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label x:Name="CustMobile" Content="Mobile No." Width="100" Margin="82 0 0 0" />
<TextBox x:Name="BoxMobile" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerMobile}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label x:Name="CustUname" Content="User Name" Width="100" Margin="82 0 0 0"/>
<TextBox x:Name="BoxUname" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerUname}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label x:Name="CustPWD" Content="Password" Width="100" Margin="82 0 0 0"/>
<TextBox x:Name="BoxPWD" Margin="2" Width="180" Text="{Binding BindCustomerData.CustomerPWD}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="82 5 0 0" >
<TextBlock x:Name="MsgText" Text="Unchanged Details are Not Saved" FontWeight="Bold"
Foreground="Red" Height="25" Margin="2" Visibility="Collapsed" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="72 5 0 0" >
<Button x:Name="CancelBtn" Content="Cancel" Width="100" Height="25" Margin="2" Click="CancelBtn_Click" />
<Button x:Name="SaveBtn" Content="Save" Width="100" Height="25" Margin="2" Visibility="{Binding SaveVisibility}" Click="SaveBtn_Click"/>
<Button x:Name="UpdateBtn" Content="Update" Width="100" Height="25" Margin="2" Visibility="{Binding UpdateVisibility}" Click="UpdateBtn_Click" />
<Button x:Name="DeleteBtn" Content="Delete" Width="100" Height="25" Margin="2" Visibility="{Binding DeleteVisibility}" Click="DeleteBtn_Click" />
</StackPanel>
</StackPanel>
Mainwindow.xaml код
<TabControl x:Name="TabControl1" Height="400" Width="650" >
<TabItem Header="View Details" Width="100" x:Name="Tab1">
<StackPanel>
<luc:UcustomerUC x:Name="ViewData" Margin="0 20 0 0"></luc:UcustomerUC>
<Button Content="UpdateDataGrid" Height="25" Width="auto" HorizontalAlignment="Right" Name="UpGridBtn"
Margin="5" Visibility="{Binding UpGridVisibility}"/>
<ScrollViewer VerticalScrollBarVisibility="Auto" >
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding CustomersData}"
ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="100" x:Name="DGdata" Margin="0 5 0 5"
SelectedItem="{Binding GetCustomerData}">
<DataGrid.Columns>
<DataGridTextColumn Header="CustomerID" Binding="{Binding CustomerID}" IsReadOnly="True" Width="*" />
<DataGridTextColumn Header="CustomerName" Binding="{Binding CustomerName}" IsReadOnly="True" Width="*"/>
<DataGridTextColumn Header="CustomerAge" Binding="{Binding CustomerAge}" IsReadOnly="True" Width="*"/>
<DataGridTextColumn Header="CustomerAddress" Binding="{Binding CustomerAddress}" IsReadOnly="True" Width="*"/>
<DataGridTextColumn Header="CustomerEmail" Binding="{Binding CustomerEmailID}" IsReadOnly="True" Width="*" />
<DataGridTextColumn Header="CustomerMobile" Binding="{Binding CustomerMobile}" IsReadOnly="True" Width="*"/>
<DataGridTextColumn Header="CustomerUserName" Binding="{Binding CustomerUname}" Visibility="Hidden" Width="*"
IsReadOnly="True"/>
<DataGridTextColumn Header="CustomerPWD" Binding="{Binding CustomerPWD}" Visibility="Hidden" Width="*"
IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</StackPanel>
</TabItem>
<TabItem Header="Add Details" Width="100" x:Name="Tab2">
<luc:UcustomerUC x:Name="AddData" Margin="0 20 0 0"></luc:UcustomerUC>
</TabItem>
<TabItem Header="Update Details" Width="100" x:Name="Tab3">
<luc:UcustomerUC x:Name="UpdateData" Margin="0 20 0 0"></luc:UcustomerUC>
</TabItem>
<TabItem Header="Delete Details" Width="100" x:Name="Tab4">
<luc:UcustomerUC x:Name="DeleteData" Margin="0 20 0 0"></luc:UcustomerUC>
</TabItem>
</TabControl>
Код Mainwindow.cs
public partial class UcustomerForm : Window
{
public UcustomerForm()
{
InitializeComponent();
ViewData.DataContext = VMvisibility.ViewDetailsVisibile.ViewDetailsBtns();
AddData.DataContext = VMvisibility.AddDetailsVisibile.AddDetailsBtns();
UpdateData.DataContext = VMvisibility.UpdateDetailsVisibile.UpdateDetailsBtns();
DeleteData.DataContext = VMvisibility.DeleteDetailsVisibile.DeleteDetailsBtns();
//CustomerVisible();
this.DataContext = new ViewModelCust();
}
public void CustomerVisible()
{
//View Tab
ViewData.SaveBtn.Visibility = Visibility.Collapsed;
ViewData.DeleteBtn.Visibility = Visibility.Collapsed;
ViewData.UpdateBtn.Visibility = Visibility.Collapsed;
ViewData.CustIDList.Visibility = Visibility.Collapsed;
//Add Tab
AddData.CustIDList.Visibility = Visibility.Collapsed;
AddData.DeleteBtn.Visibility = Visibility.Collapsed;
AddData.UpdateBtn.Visibility = Visibility.Collapsed;
//Update Tab
UpdateData.SaveBtn.Visibility = Visibility.Collapsed;
UpdateData.DeleteBtn.Visibility = Visibility.Collapsed;
//Delete Tab
DeleteData.UpdateBtn.Visibility = Visibility.Collapsed;
DeleteData.SaveBtn.Visibility = Visibility.Collapsed;
}
Пример кода модели видимости VMvisibility
public class ViewDetailsVisibile
{
static ViewDetailsVisibile details;
public Visibility CustIDVisibility
{
get { return Visibility.Collapsed; }
}
public Visibility SaveVisibility
{
get { return Visibility.Collapsed; }
}
public Visibility DeleteVisibility
{
get { return Visibility.Collapsed; }
}
public static ViewDetailsVisibile ViewDetailsBtns()
{
if (details == null)
details = new ViewDetailsVisibile();
return details;
}
}
ViewModelCust Просмотр кода модели
class ViewModelCust : INotifyPropertyChanged
{
public ObservableCollection<tblCustomerDetail> CustomersData { get; set; }
public tblCustomerDetail getCustomerData;
public tblCustomerDetail bindCustomerData;
//To get data to bind in DataGrid
public ViewModelCust()
{
FillCustomerList();
}
private void FillCustomerList()
{
PurchaseNowEntities entity = new PurchaseNowEntities();
var userlist = from r in entity.tblCustomerDetails select r;
if (CustomersData == null)
CustomersData = new ObservableCollection<tblCustomerDetail>();
foreach (var r in userlist)
{
CustomersData.Add(new tblCustomerDetail
{
CustomerID = r.CustomerID,
CustomerName = r.CustomerName,
CustomerAge = r.CustomerAge,
CustomerAddress = r.CustomerAddress,
CustomerEmailID = r.CustomerEmailID,
CustomerMobile = r.CustomerMobile,
CustomerUname = r.CustomerUname,
CustomerPWD= r.CustomerPWD
});
}
}
//To get CustomerData of selected row in DataGrid
public tblCustomerDetail GetCustomerData
{
get {
return getCustomerData;
}
set
{
getCustomerData = value;
OnPropertyChanged("GetCustomerData");
SelectedCustomer();
}
}
private void SelectedCustomer()
{
int IDCust = this.GetCustomerData.CustomerID;
PurchaseNowEntities entity = new PurchaseNowEntities();
var userdetails = entity.tblCustomerDetails.Where(x => x.CustomerID == IDCust).FirstOrDefault();
this.BindCustomerData = userdetails;
}
//To bind Customer Data to TextBoxes from Datagrid
public tblCustomerDetail BindCustomerData
{
get { return bindCustomerData; }
set
{
bindCustomerData = value;
OnPropertyChanged("BindCustomerData");
}
}
}
Пожалуйста, помогите мне, где я иду не так, а также почему данные привязываются к текстовым полям после удаления модели вида VMvisibilty.