XAML Denetim Şablon için özel bir bağımlılık özelliği ekleme

oy
4

Yalnızca bir mola biraz sonra kutuyu benim okuma ile daha da başardı ve şimdi oldukça zarif biçimde istediğiniz özelliğe sahip olması. Sorun bu onu daha iyi yapmak güzel olurdu bir felaket olmasa da ben, o iş yapmak için bir hack biraz kullanılmış olması.

Özetle: Bunun yerine tıklama etkinlik daha sonra değişken güncellenmesi neden olan bir arka plan işçi tetikler tıklandığında kendilerini check değil düzenli görünümlü onay kutusunu istiyorum. Bu değişken checkbox.ischecked bağlıdır ve daha sonra yeni değeri ile güncellenir.

Burada fikrine dayalı bir kontrol şablonu kullanmak istiyorum:

C # WPF salt okunur bir CheckBox

Ben bu değiştirilmiş ve ben (belki de denize) gerek yoktu düşünce ve ile sona erdi şeyler çıkarılır adres:

<ResourceDictionary xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:Microsoft_Windows_Themes=clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero>
<!-- -->
<Style x:Key=ReadOnlyCheckBoxStyle TargetType={x:Type CheckBox} >
        <Setter Property=Control.Template>
        <Setter.Value>
            <ControlTemplate TargetType={x:Type CheckBox}>
                <BulletDecorator SnapsToDevicePixels=true Background=Transparent>
                    <BulletDecorator.Bullet>
                        <Microsoft_Windows_Themes:BulletChrome Background={TemplateBinding Background}
                                                               BorderBrush={TemplateBinding BorderBrush}
                                                               RenderMouseOver={TemplateBinding IsMouseOver}
                                                               IsChecked={TemplateBinding Tag}>
                        </Microsoft_Windows_Themes:BulletChrome>
                    </BulletDecorator.Bullet>
                    <ContentPresenter SnapsToDevicePixels={TemplateBinding SnapsToDevicePixels}
                                      HorizontalAlignment={TemplateBinding HorizontalContentAlignment}
                                      Margin={TemplateBinding Padding}
                                      VerticalAlignment={TemplateBinding VerticalContentAlignment}
                                      RecognizesAccessKey=True />
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property=IsEnabled Value=false>
                        <Setter Property=Foreground Value={StaticResource {x:Static SystemColors.GrayTextBrushKey}} />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Böyle diyoruz ve yukarıda açıklandığı gibi bu onay kutusu çalışır:

<CheckBox x:Name=uiComboBox Content=Does not set the backing property, but responds to it. 
                  Style={StaticResource ReadOnlyCheckBoxStyle} Tag={Binding MyBoolean} Click=uiComboBox_Click/>

Yaptığım kesmek kontrolü şablonuna bağlama veri taşımak için 'Etiket' DependencyProperty kullanmaktı. Bu normalde kendini kontrol için onay kutusunu neden olan mekanizma ne olursa olsun atlar. Normal bir oyunculuk onay kutusunu geri dönmek için sadece isChecked ve BulletDecorator içine bağlayıcı bir isChecked yerine Etiketi TemplateBinding ayarlayın Etiketi bağlayıcı değiştirin.

Yani benim sorular burada sanırım:

  1. Ben yanlış sonuna sopa var mı? Ben kendini check kutusunu neden ne olursa olsun mekanizma geçersiz bir yer var mı? Belki ControlTemplate Tetikleyiciler içinde?
  2. ben sadece varsayılan CheckBox getirilen ediliyor düşünüyorum ya da ben denemek ve tüm stilleri için tam bir yedek tutmalı herhangi yedek XAML ortadan kaldırarak dolaşmak için iyi bir fikir mi?
  3. Ben ne yapıyorum Eğer çok deli değil ben Etiket özelliğini kullanmak zorunda kalmamak için ben XAML bir bağımlılık özelliği ekleyebilirim?
  4. Aynı zamanda, belki ne gerçekten istediğiniz bir onay kutusu, ben grafiğe veri bağlama üst zamanki animasyonlu onay kutusuyla belki görünmez bir düğme gibi görünen bir düğme kontrolü diye düşündüğüm oluyor. o plan üzerinde herhangi bir düşünce de çok hoş olurdu.

Çok teşekkürler

Ed

Oluştur 06/07/2009 saat 20:53
kaynak kullanıcı
Diğer dillerde...                            


1 cevaplar

oy
6

Ben Düğme etrafında dayalı özel bir denetim oluşturulan ve o zaman bir CheckBox gibi görünmesi için bir stil uygulanmış sonunda bu sorunu ve benim ReadOnlyCheckBox fikir, bozdu. Ben görüntülenen onay verisi yalnızca değişiklikleri görünecek şekilde kullanıcı tıklama set almaz ancak verilere bağlıdır kendi IsChecked özelliğini ekledi.

C #:

    public class ReadOnlyCheckBoxControl : System.Windows.Controls.Button
{
    public static DependencyProperty IsCheckedProperty;

    public ReadOnlyCheckBoxControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ReadOnlyCheckBoxControl), new FrameworkPropertyMetadata(typeof(ReadOnlyCheckBoxControl)));
    }

    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

    static ReadOnlyCheckBoxControl()
    {
        IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(ReadOnlyCheckBoxControl));
    }
}

XAML:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:y="clr-namespace:ReadOnlyCheckBoxControlNS;assembly="
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">

<SolidColorBrush x:Key="CheckBoxFillNormal" Color="#F4F4F4" />
<SolidColorBrush x:Key="CheckBoxStroke" Color="#8E8F8F" />

<Style x:Key="EmptyCheckBoxFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle SnapsToDevicePixels="true"
                           Margin="1"
                           Stroke="Black"
                           StrokeDashArray="1 2"
                           StrokeThickness="1" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="CheckRadioFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle SnapsToDevicePixels="true"
                           Margin="14,0,0,0"
                           Stroke="Black"
                           StrokeDashArray="1 2"
                           StrokeThickness="1" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type y:ReadOnlyCheckBoxControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type y:ReadOnlyCheckBoxControl}">
                <BulletDecorator SnapsToDevicePixels="true" Background="Transparent">
                    <BulletDecorator.Bullet>
                        <Microsoft_Windows_Themes:BulletChrome Background="{StaticResource CheckBoxFillNormal}"
                                                               BorderBrush="{StaticResource CheckBoxStroke}"
                                                               RenderMouseOver="{TemplateBinding IsMouseOver}"
                                                               IsChecked="{TemplateBinding IsChecked}">
                        </Microsoft_Windows_Themes:BulletChrome>
                    </BulletDecorator.Bullet>
                    <ContentPresenter SnapsToDevicePixels="True"
                                      HorizontalAlignment="Left"
                                      Margin="4,0,0,0"
                                      VerticalAlignment="Center"
                                      RecognizesAccessKey="True" />
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasContent" Value="true">
                        <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}" />
                        <Setter Property="Padding" Value="4,0,0,0" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Cevap 16/07/2009 saat 08:24
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more