< May 2009 | June 2009 | July 2009 >

June 9, 2009

RadioButton を ListBox で実装する 2

RadioButton を ListBox で実装するの続き。ListBox の IsEnabled を False に設定した際に、ListBox に設定された Background の値が無視されてしまい、なにやら悲しいことに。

以下は、Expression Blend で出力した ListBox のコントロールテンプレート。

<Custom:SolidColorBrush x:Key="ListBorder" Color="#FF7F9DB9"/>
<Style TargetType="{x:Type ListBox}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
                    <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

つまり、ListBox の ControlTemplate 自体も置き換えてやらない限り、IsEnabled が False の時の ListBox の Background はいじれないってことになる。

ええと、今忙しいのでここまで !

June 8, 2009

RadioButton を ListBox で実装する

MSDN ライブラリのバージョンに関する情報の欄に、なにげに「Microsoft Visual Studio 2010/.NET Framework 4.0」が追加されてます。

http://msdn.microsoft.com/ja-jp/library/system.windows.markup.markupextension.aspx

わたしが .NET Framework 4.0 に一番望むこととは、RadioButton へのデータバインドが正常に機能することですかね…。

フィードバック: RadioButton.IsCheckedProperty losing databindings

なにしろ、RadioButton の IsChecked に何かをバインディングすると、1つの対象に複数の RadioButton から同期を取ろうとするせいか、途中でバインディングが無効になってしまいます。

複数の項目から 1 つを選んで何かにバインドさせるなら、ListBox (というか Selector) が得意とするところですよね。ならば、見た目が RadioButton の ListBox を作ってやれば解決です。こういうのは WPF ならではですね。

続きを読む...