คำสั่ง WPF DataGrid SelectedItem ไม่เริ่มทำงาน

ฉันมีคำถามเกี่ยวกับ wpf datagrid ซึ่งเป็นพฤติกรรมของ DatagridTemplateColumn

<DataGrid
ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
inf:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectionUnit="FullRow" SelectionMode="Extended">
<DataGrid.Columns>
    <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
               <Button Background="Transparent" 
                      BorderThickness="0" 
                      BorderBrush="Transparent" 
                      Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ExecuteCommand}" CommandParameter="{Binding}"  />
           </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
 </DataGrid.Columns>

My Question is: When i press the button in DataGridTemplateColumn and the row is NOT selected, command is fired in my model. But when select my row first, command is not fired. Does anybody know why?

ขอบคุณสำหรับความช่วยเหลือของคุณ...

แก้ไข: เมื่อฉันเปลี่ยน SelectionMode จาก "Extensed" เป็น "Single" มันทำงานได้ตามที่คาดไว้ แต่ฉันต้องใช้ "ขยาย"...


person streetmaster86    schedule 21.06.2016    source แหล่งที่มา
comment
ฉันคิดว่าคุณต้องการ <DataTemplate.Triggers> และ <EventTrigger> เพื่อสิ่งเดียวกัน ขณะนี้คุณกำลังจัดการเฉพาะการคลิกปุ่มและส่งคำสั่งเพื่อดูโมเดล คุณอาจต้องคลิกทั้งแถว   -  person ViVi    schedule 21.06.2016
comment
คุณช่วยอธิบายเพิ่มเติมอีกหน่อยได้ไหม? เพราะฉันไม่เข้าใจด้วยซ้ำว่าทำไมคำสั่งจึงถูกใช้เมื่อไม่ได้เลือกแถว...   -  person streetmaster86    schedule 22.06.2016
comment
เป็นเพราะเมื่อคุณคลิกที่ปุ่ม คำสั่งของปุ่มจะทำงาน และเมื่อคุณเลือกแถว ทำไมคุณถึงคาดหวังให้คำสั่งปุ่มเริ่มทำงาน มันจะไม่ถูกไล่ออก ทำสิ่งหนึ่ง สร้างทริกเกอร์เทมเพลตข้อมูลสำหรับ PreviewMouseDown และจัดการทั้งปุ่มและแถว มันควรจะทำงาน ลองและเปลี่ยนกลับหากมีปัญหาใด ๆ   -  person ViVi    schedule 22.06.2016
comment
เรากำลังพูดถึงเรื่องเดียวกันหรือเปล่า? ฉันต้องการให้คำสั่งเริ่มทำงานเมื่อกดปุ่มเท่านั้น สิ่งนี้ทำงานได้ดีเมื่อไม่ได้เลือกแถว พอเลือกแถวก่อนแล้วกดปุ่มก็ไม่มีอะไรเกิดขึ้น...   -  person streetmaster86    schedule 23.06.2016


คำตอบ (1)


ลองใช้ IsSynchronizedWithCurrentItem="True" บน DataGrid ของคุณ เป็นสาเหตุทั่วไปที่ทำให้เกิดปัญหาที่เกี่ยวข้องกับ "SelectedItem" มิฉะนั้น คุณควรจัดเตรียมตัวอย่างโค้ดที่สมบูรณ์กว่านี้เพื่อแก้ไขปัญหานี้

person Dbl    schedule 21.06.2016