У меня был алгоритм перестановки, который я использовал для эйлера проекта, в python:
def missing(miss,src):
"Returns the list of items in src not present in miss"
return [i for i in src if i not in miss]
def permutation_gen(n,l):
"Generates all the permutations of n items of the l list"
for i in l:
if n<=1: yield [i]
r = [i]
for j in permutation_gen(n-1,missing([i],l)): yield r+j
Если
n<len(l)
вы должны иметь всю комбинацию, которая вам нужна без повторения,
Это генератор, поэтому вы используете его примерно так:
for comb in permutation_gen(3,list("ABCDEFGH")):
print comb
Вы должны установить присоединенное свойство ContextMenuService.Placement для кнопки, как указано в примечаниях к документации для ContextMenu.Placement .
<Button Name="btnFoo" Content="Foo" ContextMenuService.Placement="Bottom">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Bar" />
</ContextMenu>
</Button.ContextMenu>
</Button>
Вы пробовали это:
<Button Name="btnFoo" Content="Foo">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Bar" />
</ContextMenu>
</Button.ContextMenu>
</Button>
Это откроет ContextMenu там, где вы щелкнули правой кнопкой мыши (на кнопке). Какое, на мой взгляд, желаемое местоположение, верно?
--- ИЗМЕНИТЬ --- В этом случае используйте это:
<Button Name="btnFoo" Content="Foo" ContextMenuOpening="ContextMenu_ContextMenuOpening">
<Button.ContextMenu>
<ContextMenu Placement="Bottom">
<MenuItem Header="Bar" />
</ContextMenu>
</Button.ContextMenu>
</Button>
И в коде позади:
private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
// Get the button and check for nulls
Button button = sender as Button;
if (button == null || button.ContextMenu == null)
return;
// Set the placement target of the ContextMenu to the button
button.ContextMenu.PlacementTarget = button;
// Open the ContextMenu
button.ContextMenu.IsOpen = true;
e.Handled = true;
}
Вы можете повторно использовать метод для нескольких кнопок и ContextMenu ..