![](/img/trans.png)
[英]Binding Visible property of a DataGridColumn in WPF DataGrid
[英]DataGridColumn binding only if object property is true
我目前在嘗試在WPF中進行條件綁定時遇到問題。 我已經閱讀了該問題,似乎“可見性”並不是DataGridColumns的真正選擇,因為它不在邏輯樹視圖中。 我目前有一個對象“ Device”,其中包含對象“ Channel”的列表。 這些通道可以是輸入或輸出,表示為布爾“ isInput”。 我要完成的工作是創建兩個數據網格,一個帶有輸入,一個帶有輸出。
<DataGrid Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding Path=Channels}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Type}"
Visibility="{Binding Path=(model:Channel.IsInput),
Converter={StaticResource BooltoVisibilityConverter}}"/>
</DataGrid.Columns>
</DataGrid>
這是我目前擁有的,但是由於可見性似乎不起作用,所以我想采用一種方法在IsInput = false時隱藏整個行或完全跳過它。
如果需要多個網格,則需要根據需要過濾多個項目集合。
對於您的需求,假設通道對象的總數相對較小,我將執行以下操作。
public class ViewModel: ViewModelBase
{
public ViewModel()
{
AllChannels = new ObservableCollection<Channel>();
AllChannels.CollectionChanged += (s,e) =>
{
RaisePropertyChanged(nameof(InputChannels));
RaisePropertyChanged(nameof(OutputChannels));
}
}
private ObservableCollection<Channel> AllChanels { get; }
public IEnumerable<Channel> InputChannels => AllChannels.Where(c => c.IsInput);
public IEnumerable<Channel> OutputChannels => AllChannels.Where(c => !c.IsInput);
public void AddChannel(Channel channel)
{
AllChannels.Add(channel);
}
}
現在,您可以創建兩個網格控件並將其ItemsSource屬性綁定到InputChannels和OutputChannels。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.