Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to use value converter I would suggest you to try <code>IMultiValueConverter</code>. You can try to pass the other collection as a value and the <em>ID</em> you are looking for as two different values passed to the converter. In order to make it work, you should:</p> <ul> <li>make you code-behind variable accessible in XAML. You can find more information about some of the methods to do it here: <a href="https://stackoverflow.com/questions/666856/access-codebehind-variable-in-xaml">Access codebehind variable in XAML</a></li> <li><p>implement <code>IMultiValueConverter</code>. It can depend on some details of your application (like type of the collection you ae using), but it may look more or less like this:</p> <pre><code>class ICollectionToBoolConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { //verify if appropriate number of values is bound if (values != null &amp;&amp; values.Length == 2) { List&lt;Product&gt; productsList = (values[0] as List&lt;Product&gt;); //if converter is used with appropriate collection type if (productsList != null) { //if there is object ID specified to be found in the collection if (values[1] != null) { int objectToFindId = (int)values[1]; //return information if the collection contains an item with ID specified in parameter return productsList.Any(p =&gt; p.ID == objectToFindId); } } } //return false if object is not found or converter is used inappropriately return false; } catch { return false; } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre></li> <li><p>put your newly created converter in <code>Resources</code> of <code>Window</code> (or <code>UserControl</code>) where the <code>DataGrid</code> resides</p> <pre><code>&lt;c:ICollectionToBoolConverter x:Key="collectionToBoolConverter" /&gt; </code></pre></li> <li><p>bind the <code>CheckBox</code> using the converter, it can depend on specific way you use to expose the other collection (as mentioned in the first step of this answer). However, it may look similar to this:</p> <pre><code>... &lt;Custom:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;CheckBox HorizontalAlignment="Center" Style="{DynamicResource myCheckBoxStyle}"&gt; &lt;CheckBox.IsChecked&gt; &lt;MultiBinding Converter="{StaticResource collectionToBoolConverter}"&gt; &lt;Binding ElementName="layoutRoot" Path="Parent.MyCollectionName" /&gt; &lt;Binding Path="ID" /&gt; &lt;/MultiBinding&gt; &lt;/CheckBox.IsChecked&gt; &lt;/CheckBox&gt; &lt;/DataTemplate&gt; &lt;/Custom:DataGridTemplateColumn.CellTemplate&gt; ... </code></pre></li> </ul> <p>I haven't tested this, so if you have any issues with any of those tasks, let me know and I can try to help you.</p>
 

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