以下は、私が望むようにほとんど機能するコードです。私はそれを取得しようとしている行をクリックし、私はprogramIDを取ると、別の場所で使用する、この部分は動作します。しかし、コンボボックスが組み込まれているステータスカラムをクリックすると、何も起こりません。どのようにしてこのカラムを使用するのを制限しますか?それがここにXAML Datagrid、一部の列に対してのみ選択された値
<DataGrid x:Name="gridResult" IsSynchronizedWithCurrentItem="True" SelectedValue ="{Binding ModelRequestSV}" Grid.Row="2" Grid.RowSpan="5" Grid.ColumnSpan="4" IsReadOnly="TRue" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" CanUserAddRows="false" AutoGenerateColumns="False" ItemsSource="{Binding ModelRequestObs}" HorizontalGridLinesBrush="Gray" GridLinesVisibility="Horizontal" Style=" {DynamicResource DataGridStyle1}" Margin="0,25,0,10" >
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding DoubleClickCommand}"
CommandParameter="{Binding ModelRequestSV}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ProgramID}" Header="Program ID" Width="100" />
<DataGridTextColumn Binding="{Binding Cedant}" Header="Cedant" Width="300" />
<DataGridTextColumn Binding="{Binding UWCode}" Header="Underwriter" Width="145"/>
<DataGridTextColumn Binding="{Binding DateSubmitted}" Header="Date Submitted" Width="145"/>
<DataGridTextColumn Binding="{Binding RequiredDate}" Header="Required Date" Width="145"/>
<DataGridTextColumn Binding="{Binding Priority}" Header="Priority" Width="145"/>
<DataGridTemplateColumn Header="Status" Width="140">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="110" Text="{Binding Status}" />
<ComboBox Text="{Binding Status}"
Width="20" Height="20"
BorderThickness="0"
SelectedItem="{Binding Status}"
ItemsSource="{Binding DataContext.StatusCodes, ElementName=userControl}"
SelectionChanged="DataGrid_SelectionChanged">
</ComboBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
を必要とされている場合、私はMVVMを使用しています私のviewmodelロジックは
private ModelRequest _modelRequestSV;
private void NotifyProgrIdToParent()
{
CAT_Application_WPF.UI.App.MainViewModel.ProgIdCommand.Execute(_modelRequestSV.ProgramID.ToString());
}
public ModelRequest ModelRequestSV
{
get { return _modelRequestSV; }
set
{
if (value != _modelRequestSV)
{
_modelRequestSV = value;
OnPropertyChanged(nameof(ModelRequestSV));
if (ModelRequestSV != null)
{
NotifyProgrIdToParent();
}
}
}
}
試みです。 firstSelectedCell.Column.DisplayIndex.ToString()はどこでクリックしても常に「0」を返しますが、ここで問題は何ですか?あなたのイベントハンドラDataGrid_SelectionChangedで
private void DataGrid_MouseDoubleClick(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
System.Windows.Controls.DataGrid myGrid = (System.Windows.Controls.DataGrid)sender; // now this is strongly typed so we can drill into its properties.
var vm = (NotesManagerViewModel)DataContext;
var selectedCells = myGrid.SelectedCells; // there can be more than one cell selected! Which column is that in?
if (selectedCells.Count > 0)
{
DataGridCellInfo firstSelectedCell = selectedCells[0]; // usually there will be only one selected. If not, think about how you want to handle this.
DataGridColumn ignoreThisColumn = myGrid.Columns[7]; // whichever column you want to ignore - you could also have several columns in an array...
if (firstSelectedCell.Column.DisplayIndex.ToString() != ignoreThisColumn.DisplayIndex.ToString())
{
vm.NotifyProgrIdToParent();
}
}
}
私は現在のロジックが何であるかを編集します。 VMからロジックを取り出してそこに配置することはできますか? – James
VM内のコードでダブルクリックが処理され、コードビハインド(SelectionChanged = "DataGrid_SelectionChanged")によって処理されるSelectionChangedがあるようです。この回答はコードビハインドにも当てはまります。あなたのVMでチェックが行われるようにするには、ModelRequestSVだけで作業する必要があります。あなたがそれを把握できるかどうかは分かりません。あなたのVMからは、ビューの直接の知識はありません(MVVMに違反することもありません)。 「クリックされた列」はビューに依存するため、このタイプのロジックはおそらくコードの背後にあります。 –
Puristsは3 ... 2 ... 1 ... –