我使用this answer將ListBox
設置爲RadioButton
,以便通過驅動通過枚舉選擇單選按鈕來簡化我的MVVM代碼 - 而不是使用一堆布爾手動來回映射到枚舉。ContentControl在風格化列表框中的垂直對齊
當ListBoxItem
內容是簡單的文本行時,此功能非常有用。單選按鈕與文字對齊,所有人都很開心。但是當我將內容更改爲UserControl
時,該選項的單選按鈕將在UserControl
的垂直中點處呈現,而不是頂部(我需要它的位置)。
下面是一些代碼和圖像,更好地解釋什麼,我試圖做(注意的東西,一堆被忽略了爲清楚起見):
的UserControl
,我將作爲內容進入一個的選擇
<UserControl x:Class="TestCtl">
<StackPanel Orientation="Vertical" >
<Label Margin="-5,0,0,0" Content="Choice #2"/>
<CheckBox Margin="10,0,0,5">Option 1</CheckBox>
<CheckBox Margin="10,0,0,5">Option 2</CheckBox>
<CheckBox Margin="10,0,0,0">Option 3</CheckBox>
</StackPanel>
</UserControl>
的ListBox
(與其它地方定義上述風格)
<StackPanel Orientation="Vertical">
<ListBox SelectedValuePath="Tag"
Style="{StaticResource RadioButtonList}"
SelectedValue="{Binding Blah Blah"}>
<ListBoxItem Tag="Choice1" Content="Choice #1" />
<ListBoxItem Tag="Choice2">
<ContentControl>
<subf:TestCtl />
</ContentControl>
</ListBoxItem>
<ListBoxItem Tag="Choice3" Content="Choice #3"/>
<ListBoxItem Tag="Choice4" Content="Choice #4" />
</ListBox>
<ComboBox blah blah/>
</StackPanel>
什麼樣子李柯渲染時:
我試圖同時設置VerticalAlignment
和VerticalContentAlignment
以及與Margin
和Padding
在每個位置都打,我可以在我的兩個XAML代碼和我聯繫的風格想到,但無論我設置了什麼,我都無法讓單選按鈕和用戶控件在頂部對齊。
有無論如何通過修改我使用的樣式或我的代碼來實現我想要的嗎?或者我只是平淡地做這個錯誤?
將ListBox設置爲RadioButton?你應該閱讀:https://stackoverflow.com/questions/37790017/wpf-mvvm-radiobutton-handle-binding-with-single-property – mm8
@ mm8有趣的是,我真的看到了第一次今天和以前的第一次已經多次使用了MVVM Radio按鈕。我可能會嘗試這種方法,但我想知道我是否仍然會遇到相同的對齊問題。 –