Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reuse a UserControl in situations where the DataContext is slightly different?
    primarykey
    data
    text
    <p>This is my first post on StackOverflow and also my first Question. I had created a UserControl in WPF with the MVVM Pattern (called Block). This UserControl should be used in another UserControl, as DataTemplate of a ListBox (called Sector).</p> <p>The Block Control should be used as standalone and as DataTemplate for different UserControls.</p> <p>If I set the DataContext in the View of Block, the standalone Version works great, but not as part of the Sector View.</p> <p>If I don´t set the DataContext in the View of Block, it works in Sector but don’t standalone.</p> <p>My question is, is it the only way to leave the DataContext in the View of Block and set it in the View I used the Control or the ViewModel?</p> <p>Here is my Code:</p> <p><strong>Model of Block:</strong></p> <pre><code>public class BlockModel : ModelBase { #region private Variables string blockName = "Block"; string blockContent = "00000"; #endregion private Variables #region Properties public string BlockName { get { return blockName; } set { blockName = value; NotifyPropertyChange("BlockName "); } } public string BlockContent { get { return blockContent; } set { blockContent = value; NotifyPropertyChange("BlockContent"); } } #endregion Properties #region ctor public BlockModel () { } #endregion ctor } </code></pre> <p><strong>ViewModel of Block:</strong></p> <pre><code>public class BlockViewModel : ViewModelBase { #region private Variables string charToFill = "0"; DelegateCommand fillWithChar; BlockModel dataModel = new BlockModel(); #endregion private Variables #region Properties public Models. BlockModel DataModel { get { return dataModel; } set { dataModel = value; } } public string BlockContent { get { return dataModel. BlockContent; } set { if (dataModel. BlockContent != value) { dataModel. BlockContent = value; NotifyPropertyChange ("BlockContent"); } } } public string BlockName { get { return dataModel. BlockName; } set { if (dataModel. BlockName != value) { dataModel. BlockName = value; NotifyPropertyChange("BlockName"); } } } public string CharToFill { get { return charToFill; } set { if (charToFill != value) { charToFill = value; NotifyPropertyChange ("CharToFill"); } } } public ICommand FillWithChar { get { if (fillWithChar == null) fillWithChar = new DelegateCommand(FillText); return fillWithChar; } } #endregion Properties #region ctor public BlockViewModel() { } #endregion ctor #region Methods private void FillText() { CodingBlockContent = CodingBlockContent.PadLeft(32, charToFill[0]); } #endregion Methods </code></pre> <p>}</p> <p><strong>View of Block:</strong></p> <pre><code>&lt;UserControl x:Class="…./Block" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:…/Controls" mc:Ignorable="d" d:DesignWidth="460" d:DesignHeight="44"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="124" /&gt; &lt;ColumnDefinition Width="301" /&gt; &lt;ColumnDefinition Width="30"/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="30"&gt;&lt;/RowDefinition&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding BlockName}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14" Margin="2,6"/&gt; &lt;ComboBox FontFamily="Consolas" Grid.Row="00" Grid.Column="1" Text="{Binding BlockContent, Mode=TwoWay}" Tag="{Binding BlockName}" Name="cbxBlock" FontSize="14" HorizontalAlignment="Left" VerticalAlignment="Center" Width="290" Margin="1,4,0,5" IsEditable="True" Height="26"&gt; &lt;ComboBoxItem Content=""&gt;&lt;/ComboBoxItem&gt; &lt;ComboBoxItem Content="0000000000000"&gt;&lt;/ComboBoxItem&gt; &lt;StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal"&gt; &lt;TextBox Margin="10,2,0,2" Text="0" Width="24" FontSize="14" Name="tbxChar" MaxLength="1"&gt;&lt;/TextBox&gt; &lt;TextBlock Margin="10,2,0,2" Text="auffüllen" VerticalAlignment="Center" FontSize="14"&gt;&lt;/TextBlock&gt; &lt;Button Margin="10,2,0,2" Content="Ausführen" Width="100" Command="{Binding FillWithChar}"&gt;&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/ComboBox&gt; &lt;TextBlock Grid.Row="0" Width="30" Grid.Column="2" Text="{Binding CodingBlockContent.Length}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14" Margin="2,6,0,6" Grid.ColumnSpan="2"/&gt; &lt;/Grid&gt; </code></pre> <p></p> <p><strong>Model of Sector:</strong></p> <pre><code>public class SectorModel { #region private Variables int blockAmount = 4; string sectorNumber = "Sektor"; int blockBegin = 0; bool isSectorInUse = false; #endregion private Variables #region Properties public string SectorNumber { get { return sectorNumber; } set { sectorNumber = value;} } public int BlockBegin { get { return blockBegin; } set { blockBegin = value; } } public bool IsSectorInUse { get { return isSectorInUse; } set { isSectorInUse = value;} } public int BlockAmount { get { return blockAmount; } set { blockAmount = value;; } } #endregion Properties public SectorModel() { } } </code></pre> <p><strong>ViewModel of Sector:</strong></p> <pre><code>public class SectorViewModel : ViewModelBase { #region private Variables SectorModel dataModel = new SectorModel(); ObservableCollection&lt;BlockViewModel&gt; sectorBlocks = new ObservableCollection&lt;BlockViewModel&gt;(); #endregion private Variables #region Properties public SectorModel DataModel { get { return dataModel; } set { dataModel = value; } } public ObservableCollection&lt;BlockViewModel&gt; SectorBlocks { get { return sectorBlocks; } set { sectorBlocks = value; NotifyPropertyChange ("SectorBlocks"); } } public string SectorNumber { get { return "Sektor " + DataModel.SectorNumber; } set { DataModel.SectorNumber = value; NotifyPropertyChange ("SectorNumber"); } } public int BlockBegin { get { return DataModel.BlockBegin; } set { DataModel.BlockBegin = value; SetBlocks(); OnPropertyChanged("BlockBegin"); } } public bool IsSectorInUse { get { return DataModel.IsSectorInUse; } set { DataModel.IsSectorInUse = value; NotifyPropertyChange ("IsSectorInUse"); } } public int BlockAmount { get { return DataModel.BlockAmount; } set { DataModel.BlockAmount = value; SetBlocks(); NotifyPropertyChange ("CodingBlockAmount"); } } #endregion Properties void SetBlocks() { while (SectorBlocks.Count != BlockAmount) { SectorBlocks.Add(new BlockViewModel()); } int begin = BlockBegin; foreach (BlockViewModel block in SectorBlocks) { block.CodingBlockName = "Block " + begin.ToString().PadLeft(2, '0'); block++; } } public SectorViewModel() { SetBlocks(); } } </code></pre> <p><strong>View of Sector:</strong></p> <pre><code>&lt;UserControl xmlns:Views="clr-namespace:…/RFIDControls" x:Class="…/Sector" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="473"&gt; &lt;Border BorderBrush="Black" BorderThickness="0,0,0,1"&gt; &lt;Expander Name="expMain" Margin="0,0,4,0"&gt; &lt;Expander.Header&gt; &lt;Grid Name="grdHeader"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="300" /&gt; &lt;ColumnDefinition Width="30" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0" Grid.Row="0" Height="24" Text="{Binding SectorNumber}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14" FontWeight="Bold" Panel.ZIndex="98"/&gt; &lt;CheckBox Grid.Column="2" VerticalAlignment="Center" IsChecked="{Binding IsSectorInUse}"&gt;&lt;/CheckBox&gt; &lt;/Grid&gt; &lt;/Expander.Header&gt; &lt;Grid&gt; &lt;ListBox ItemsSource="{Binding SectorBlocks}" Name="listBox"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type Views:BlockViewModel}"&gt; &lt;Views:Block/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/Expander&gt; &lt;/Border&gt; </code></pre> <p></p> <p>Thank you.</p> <p>With kind regards from Germany</p> <p>Dominik</p> <p>PS: I hope my English is not so bad as I suppose</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload