Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c代码背后的编写风格#_C#_Wpf_Visual Studio 2012_Coding Style_Resources - Fatal编程技术网

C# c代码背后的编写风格#

C# c代码背后的编写风格#,c#,wpf,visual-studio-2012,coding-style,resources,C#,Wpf,Visual Studio 2012,Coding Style,Resources,我在resorces.xaml中编写了这段代码,以便在我的项目中应用它: <Style TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid>

我在resorces.xaml中编写了这段代码,以便在我的项目中应用它:

 <Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Rectangle x:Name="rect" Style="{DynamicResource rectangle_style}" Cursor="Hand">
                        <Rectangle.Fill>
                            <ImageBrush ImageSource="Attempts\image.jpg" Stretch="UniformToFill"/>
                        </Rectangle.Fill>
                    </Rectangle>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True"/>
                    <Trigger Property="IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True"/>
                    <Trigger Property="IsPressed" Value="True"/>
                    <Trigger Property="IsEnabled" Value="False"/>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


我会从C#中的代码隐藏中编辑这种样式,因为我想从代码中动态更改imagesource,如何从C#中的代码隐藏中编写它?

虽然我没有时间逐行翻译,但我已经写了一篇关于如何在代码隐藏中创建数据模板/样式的完整文章

那应该让你开始

尽管像其他人所说的那样,我认为您应该采用ViewModel类型的方法,并让绑定来实现

不过,另一种方法是劫持一个非常未使用的属性(或者更好地添加一个附加的属性),例如Tag,并将其用作图像名称。然后你可以创建一个ControlTemplate,它可以是你的样式的一部分,它会在特定的按钮(你正在应用样式的那个按钮)中查找标签,标签是图像的路径

比如说:

<ControlTemplate x:Key="bordereredButtonTemplateWithMouseAndPropHiJacking" 
            TargetType="{x:Type Button}">
    <Border x:Name="border" CornerRadius="3" 
            Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding Foreground}" 
            BorderThickness="2" Width="auto" 
            Visibility="Visible">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent},
                 Path=Tag}" Width="20" 
                 Height="20" HorizontalAlignment="Left"
                 Margin="{TemplateBinding Padding}" />
            <ContentPresenter  
                Margin="{TemplateBinding Padding}" 
                Content="{TemplateBinding Content}" 
                Width="auto" Height="auto"/>
        </StackPanel>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter TargetName="border" 
                Property="Opacity" Value="0.4"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter TargetName="border" 
                Property="Background" Value="Orange"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

然后,当您想在XAML中使用它时,您可以这样做

<Button Template="{StaticResource bordereredButtonTemplateWithMouseAndPropHiJacking}"   
Tag="c:\temp\image1.jpg"/>
<Button Template="{StaticResource bordereredButtonTemplateWithMouseAndPropHiJacking}" 
Tag="c:\temp\image1.jpg"/>


对于您来说,您可能希望使模板成为样式的一部分,但您得到了一个想法,我希望不要在代码中操纵UI元素。如果您需要动态ImageSources,请在ViewModel中的某个位置放置
公共字符串ImageSource{get;set;}
,并将图像绑定到该位置;处理WPF ui元素的创建(以及所有相关的结构)是一项繁重而脆弱的任务,尽管你欺骗了所有人。。。我睡觉的时候你在棋盘上移动了两块。当我得到一些信息时,我会继续这样做time@HighCore哈-这就是当我无法入睡时会发生的事情:我喝苏格兰威士忌,然后随机编码对不起,我只想更改一张图片!奥萨查,你是我的偶像之一,但我不同意你的看法。即使OP除了在代码中这样做之外没有其他机会,首选的方法仍然是使用XAML和XAMLReader,就像我实际上同意你的观点一样,我认为XAML方法是可行的。然而,因为他问我代码中的风格,我只是向他指出了一个资源。我认为他要么去MVVM,要么拥有一个保存按钮对象列表的迷你vm。或者使用你的方法,这也是很酷的